2011-01-12 136 views
2

我试图让我的UITableViewCell的字体大小变小。这里是我的代码:UITableViewCell字体大小不变

- (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]; 

     mySwitch = [[[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"; 


} 

但标签的字体肯定是不出来是大小5.这是正常的字体和大小正常,大概是12号字体,或任何默认为。为什么我无法更改字体大小?

+0

有没有您要添加的nameLabel到该单元的内容查看两次理由吗? – 2011-01-12 15:39:47

回答

0

尝试此相反的nameLabel.textLabel.text = @"Hello";

使用

nameLabel.text = @"Hello"; 
2

完全无关的问题,但没有必要将开关添加作为一个子视图。设置附件视图应该将其添加到单元格并保留它。

[cell addSubview:mySwitch]; 
    cell.accessoryView = mySwitch; 

...应该只是......

[cell setAccessoryView:mySwitch];