【发布时间】:2012-02-20 08:44:16
【问题描述】:
我得到了带有 UITableView 的应用程序。
每个 UITableViewCell 中都有 UILabel。我是这样添加标签的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//look at the upd
}
但是当我尝试使用其他信息重新加载此表中的数据时,旧信息(旧标签)不会消失,但仍在单元格上。
看起来像这样:...
我应该如何组织添加 UILabel?
更新
我以这种方式更正代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Recent Dream";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *textLabel = [[UILabel alloc] init];
textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
textLabel.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont boldSystemFontOfSize:16];
[textLabel setTag:(int)indexPath];
[cell.contentView addSubview:textLabel];
}
// Configure the cell...
Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];
cell.contentView.backgroundColor = background;
UILabel *textLabel = (UILabel *)[cell viewWithTag:(int)indexPath];
textLabel.text = tempDream.title;
NSLog(@"%@",tempDream.title);
NSLog(@"%@",textLabel.text);
cell.textLabel.text = @"";
cell.detailTextLabel.text = stringFromDate;
return cell;
}
输出如下所示:
2012-02-20 15:58:10.512 DreamerIOS[3037:10403] lllooooonggg dreeeeeeeaaaaammmm yeeeeeeah
2012-02-20 15:58:10.513 DreamerIOS[3037:10403] (null)
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] dream to end
2012-02-20 15:58:10.516 DreamerIOS[3037:10403] (null)
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] adsfhadskgh dfagfadkuy gadyv dsfdfad fsdf a ghfncdhg hjfg f dfj ghf
2012-02-20 15:58:10.519 DreamerIOS[3037:10403] (null)
所以,NSLog(@"%@",tempDream.title);可以,但是 NSLog(@"%@",textLabel.text);一片空白。为什么?
【问题讨论】:
标签: ios xcode uitableview uilabel