UITableViewCell font size is not changing
I'm trying to get the font size on my UITableViewCell to be smaller. Here is the code I have:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* CellIdentifier = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
UILabel *nameLabel;
UISwitch *mySwitch;
if ( cell == nil )
{
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: CellIdentifier] autorelease];
nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake( 220, 13, 100, 20 )] autorelease];
nameLabel.tag = 22;
nameLabel.font = [UIFont boldSystemFontOfSize: 5.0];
nameLabel.textColor = [UIColor blueColor];
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
nameLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: nameLabel];
my开发者_如何学CSwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
mySwitch.tag = ((400*(indexPath.section+1))+indexPath.row);
[mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:mySwitch];
cell.accessoryView = mySwitch;
}
else {
nameLabel = (UILabel*)[cell.contentView viewWithTag: 22];
mySwitch = (UISwitch *)[cell.contentView viewWithTag:((400*(indexPath.section+1))+indexPath.row)];
}
nameLabel.textLabel.text = @"Hello";
}
But the font of the label is certainly not coming out to be size 5. It's normal font and normal size, probably about font size 12, or whatever the default is. Why can't I change the font size?
Unrelated to your issue, but there's no need to add the switch as a subview. Setting the accessory view should add it to the cell and retain it.
[cell addSubview:mySwitch];
cell.accessoryView = mySwitch;
...should just be...
[cell setAccessoryView:mySwitch];
Try this
Instead of nameLabel.textLabel.text = @"Hello";
Use
nameLabel.text = @"Hello";
精彩评论