我应该在cellForRowAtIndexPath:
方法的cell == nil
部分设置cell.titleLabel
的字体吗?还是之后?我还以编程方式添加了一些标签和一个UIImage
。UIImage
不会改变,但标签的值会改变。
- (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;
}
谢谢你的帮助。
发布于 2013-02-05 11:40:23
您在大括号中设置字体是正确的,因为这段代码应该执行一次。大括号应该是访问您的数据源的代码,例如,当您像这样执行smth时,cell.label.text = [self.dataArray objectAtIndex:i];
- (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;
}
发布于 2013-02-05 11:40:36
如果单元格字体与行号无关,那么它总是必须放在if (单元格== nil)中。
https://stackoverflow.com/questions/14706553
复制