gpt4 book ai didi

iphone - 重新加载时,UILabel 在 UITableViewCell 中不刷新

转载 作者:行者123 更新时间:2023-12-01 18:03:07 24 4
gpt4 key购买 nike

我加了两个 UILabelsUITableViewCell然后为该单元格命名和编号标签。当我重新加载 UITableView 中的数据时(无论我从 TableView 中添加还是删除联系人)标签中的数据是重叠的,这意味着它不会重新加载标签,直到重新加载整个 View 。

有人可以帮忙吗?

代码:

- (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;
}

最佳答案

您应该在单元初始化中移动标签创建代码,然后稍后通过标签引用它们,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

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);
UILabel *name = [[UILabel alloc] initWithFrame:Label1Frame];
name.tag = 1;
[name setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:name];
[name release];

CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
UILabel *number = [[UILabel alloc] initWithFrame:Label2Frame];
number.tag = 2;
[number setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:number];
[number release];
}

UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
UILabel *numberLabel = (UILabel *)[cell viewWithTag:2];
name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];

return cell;
}

关于iphone - 重新加载时,UILabel 在 UITableViewCell 中不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com