在动态表中重新加载 TableViewCell 的 UILabels [英] Reload TableViewCell's UILabels in dynamic table
问题描述
我得到了带有 UITableView 的应用程序.
I got app with UITableView.
每个 UITableViewCell 中都有 UILabel.我以这种方式添加标签:
There is UILabel in each UITableViewCell. I add labels in this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//look at the upd
}
但是当我尝试使用其他一些信息重新加载此表中的数据时,旧信息(旧标签)不会消失,但仍在单元格上.
But when i try to reload data in this table with some other information, old infromation (old labels) dont disappear, but still on cells.
看起来像这样:...
我应该如何组织添加 UILabels?
How should i organized adding UILabels?
更新
我以这种方式更正代码:
I correct code in this way:
- (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);一片空白.为什么?
So, NSLog(@"%@",tempDream.title); is OK, but NSLog(@"%@",textLabel.text); is null. Why?
推荐答案
这是因为每次给单元格添加一个新标签.
It is because each time you add a new label to the cell.
当您初始化一个新单元格时,您添加标签并为其设置标签
When you init a new cell, you add the label and set a tag to it
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// add label here
...
[label setTag:999];
}
然后在配置cell的时候,使用[UIView viewWithTag:]来获取label的引用
Then when you configure the cell, use [UIView viewWithTag:] to get the reference of the label
UILabel *label = (UILabel *)[cell.contentView viewWithTag:999];
更新:
您应该为标签使用常量值.如果使用 indexPath 作为标签,则可能找不到视图.由于您正在重复使用单元格,系统只会创建一定数量的单元格.当单元格不在视图中(不可见)时,dequeueReusableCellWithIdentifier
将为您获取单元格而不是创建新单元格.
You should use a constant value for the tag. If you use the indexPath as the tag, the view may not found. Since you are reusing the cell, the system only create a certain number of cells. When the cell is out of view (not visible), dequeueReusableCellWithIdentifier
will get the cell for you rather than create a new cell.
reusableCell 是您之前创建的视图.您为标签使用动态值的情况将如下所示:您在单元格 10(indexPath) 处,系统找到在 3(indexPath) 处创建的 reusableCell.因为它是在3创建的,所以label标签是3.但是你找到一个带有标签10的视图,因此结果为空.
The reusableCell is a view that you create before. The case you use a dynamic value for the tag will like this: You are at cell 10(indexPath), the system find a reusableCell which is created at 3(indexPath). Since it was created at 3, the label tag is 3. But you find a view with tag 10, therefore the result is null.
这篇关于在动态表中重新加载 TableViewCell 的 UILabels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!