首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在cellForRowAtIndexPath:方法中,cell.titleLabel的字体设置应该放在哪里?

在cellForRowAtIndexPath:方法中,cell.titleLabel的字体设置应该放在哪里?
EN

Stack Overflow用户
提问于 2013-02-05 19:32:54
回答 2查看 461关注 0票数 1

我应该在cellForRowAtIndexPath:方法的cell == nil部分设置cell.titleLabel的字体吗?还是之后?我还以编程方式添加了一些标签和一个UIImageUIImage不会改变,但标签的值会改变。

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *identifier = @"identifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

 [cell.titleLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 15.0f]];
      [cell.descriptionLabel setFont:[UIFont fontWithName: @"Asap-Regular" size: 10.0f]];

  }

  **// or should it go here?**

  return cell;
}

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-05 19:40:23

您在大括号中设置字体是正确的,因为这段代码应该执行一次。大括号应该是访问您的数据源的代码,例如,当您像这样执行smth时,cell.label.text = [self.dataArray objectAtIndex:i];

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *identifier = @"identifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  if (cell == nil) {
     //executed once per cell
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault          reuseIdentifier:identifier] autorelease];
    [cell.titleLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 15.0f]];
    [cell.descriptionLabel setFont:[UIFont fontWithName: @"Asap-Regular" size: 10.0f]];
  }
//Executed every time
 cell.label.text = [self.dataArray objectAtIndex:i];
  return cell;
}
票数 3
EN

Stack Overflow用户

发布于 2013-02-05 19:40:36

如果单元格字体与行号无关,那么它总是必须放在if (单元格== nil)中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14706553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档