【问题标题】:UITableViewCell Font Not ChangingUITableViewCell 字体不变
【发布时间】:2009-12-13 02:57:37
【问题描述】:

我正在尝试使用以下代码自定义 UITableViewCell 的字体,以便在填充 tableview 时使用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }

    // Set up the cell  
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];   
    NSString *temp = [[NSString alloc] initWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
    cell.textLabel.text = temp;
    [[cell textLabel] setTextColor:[UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1]];
    [[cell textLabel] setFont:[UIFont systemFontOfSize:12.0]];  

    return cell;
}

对于我的生活,我不知道为什么它不会改变字体!如果我在单元格文本中硬编码,例如cell.textLabel.text = @"TEST";

,上面的代码也可以正常工作

有什么建议吗?谢谢!

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    首先,您应该自动释放您的单元格。你现在正在疯狂地泄漏内存。

    其次,你应该更新tableView:willDisplayCellForRow:atIndexPath:中的字体。如果您使用的是标准表格视图,它将(随机)更改您的单元格,并且您需要在 tableView:willDisplayCellForRow:atIndexPath: 中而不是在数据源方法中进行字体更改、背景颜色等操作。

    另请参阅此线程:What is -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?

    【讨论】:

    • 能否请您详细说明我应该更改它的方法?谢谢!
    【解决方案2】:

    @Jason 可可:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    }
    

    【讨论】:

      【解决方案3】:

      你可以试试下面的代码。

      
      -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          static NSString *cellString = @"cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
          if(cell == nil){
              cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellString];
          }
          cell.textLabel.text = values;
          cell.textLabel.textColor = [UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1];
          cell.textLabel.font = [UIFont systemFontOfSize:12.0];
          return cell;
      }

      【讨论】:

      • 尝试使用 cell.textLabel.font 或 cell.detailTextLabel.font
      【解决方案4】:

      试试,

      cell.textLabel.font = [UIFont systemFontOfSize:12.0];
      cell.detailTextLabel.font = [UIFont systemFontOfSize:10.0];
      

      【讨论】:

        【解决方案5】:

        willDisplayCell:forRowAtIndexPath: 可用于修改单元格的背景颜色、内容视图等,除了textlabel和detailtextlabel的backgroundColor属性

        在上述方法中进行这些修改总是一个好习惯

        【讨论】:

          【解决方案6】:

          很简单,`

          - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
          {
          
             cell.textLabel.textColor = [UIColor grayColor]; //redColor greenColor etc.
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-03-29
            • 2011-06-07
            • 1970-01-01
            • 2015-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多