重新加载时 UILabel 不会在 UITableViewCell 中刷新
我将两个 UILabels
添加到 UITableViewCell
中,然后向该单元格添加名称和数字标签。当我重新加载 UITableView
中的数据时(无论是从表视图中添加还是删除联系人),标签中的数据是重叠的,这意味着它不会重新加载标签,直到重新加载整个视图。
有人可以帮忙吗?
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UILabel *name;
UILabel *number;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
// NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
// [sortDescriptor release];
}
CGRect Label1Frame = CGRectMake(10, 10, 140, 25);
name = [[UILabel alloc] initWithFrame:Label1Frame];
name.tag = 1;
[name setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:name];
CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
number = [[UILabel alloc] initWithFrame:Label2Frame];
number.tag = 1;
[number setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:number];
name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];
[name release];
[number release];
return cell;
}
I add two UILabels
to a UITableViewCell
and then name and number labels to that cell. When I reload the data in the UITableView
(whether I add or delete a contact from the table view) the data in the labels are overlapping which means it's not reloading the labels, until the entire view is reloaded.
Can anyone please help?
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UILabel *name;
UILabel *number;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
// NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
// [sortDescriptor release];
}
CGRect Label1Frame = CGRectMake(10, 10, 140, 25);
name = [[UILabel alloc] initWithFrame:Label1Frame];
name.tag = 1;
[name setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:name];
CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
number = [[UILabel alloc] initWithFrame:Label2Frame];
number.tag = 1;
[number setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:number];
name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];
[name release];
[number release];
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将标签创建代码移至单元初始化内部,然后稍后通过标签引用它们,如下所示:
You should move your label creation code inside the cell initialization and then just reference them later by tag like this:
您没有正确地重复使用 UITableViewCell 对象,您只需要为每个需要创建的单元格分配和设置 UILabel 对象一次。如果单元格已经存在并且可以重复使用,那么您只需要检索所需的 UILabel 对象并修改每个对象的文本属性:
我建议阅读以下内容:
UITableView 类参考
You are not re-using your UITableViewCell objects correctly, you only need to allocate and setup your UILabel objects once per cell that needs to be created. If a cell does already exist and can be re-used then you just need to retrieve the desired UILabel objects and modify the text property for each:
I suggest reading the following:
UITableView class reference
最简洁的方法是创建自定义表格视图单元格,并仅在
CellForRowAtIndexPath
方法中设置标签值。这确保了 UITableViewCell 的最有效使用和重用。
The cleanest way to do it is create custom table view cells and only set the label values in the
CellForRowAtIndexPath
method.That ensures the most efficient use and reuse of
UITableViewCell
s.