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

标签 iphone objective-c ios uitableview uilabel

我加了两个 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/

相关文章:

iphone - stringWithContentsOfURL 和 HTTP 500 错误代码

iPhone - 将 MKPolylineView lineDashPattern 设置为动画

objective-c - Objective-C 弧 : How to remove an object from an array/set and then return it as an autoreleased object?

ios - 使用popViewController自定义过渡时如何防止键盘关闭

用于管理 Apple 推送通知的 Java 库

iphone - 如何为 uitoolbar 设置动画

objective-c - 为什么 fileManager copyItemAtPath :ToPath:error return an error, 成功复制文件时?

Objective-C if ([self isKindOfClass : . .. ]) 静态方法

ios - collectionView 单元格重叠

ios - 快速定时器 SK.wait