重新加载时 UILabel 不会在 UITableViewCell 中刷新

发布于 2024-10-21 13:37:28 字数 1611 浏览 6 评论 0原文

我将两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有你我更好 2024-10-28 13:37:28

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

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

You should move your label creation code inside the cell initialization and then just reference them later by tag like this:

- (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;    
}
吹梦到西洲 2024-10-28 13:37:28

您没有正确地重复使用 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

蓝天白云 2024-10-28 13:37:28

最简洁的方法是创建自定义表格视图单元格,并仅在 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 UITableViewCells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文