【问题标题】:problem with cellForRowAtIndexPath method in UITableView iphone (indexPath.row is null)UITableView iphone 中 cellForRowAtIndexPath 方法的问题(indexPath.row 为空)
【发布时间】:2011-04-03 19:05:26
【问题描述】:

过去两天,表格视图单元格管理让我抓狂。请检查下面的代码,我会详细解释你的问题..

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


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *nameLabel,*sugarLabel,*searchNameLabel,*searchSugarLabel;

    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
        CGFloat height = 20;
        CGRect frame = CGRectMake(10.0f, 10.0f, width, height);
        if(isSearchOn)
        {
            searchNameLabel = [[UILabel alloc] initWithFrame:frame];
            searchNameLabel.textColor = [UIColor blackColor];
            searchNameLabel.backgroundColor = [UIColor clearColor];
            searchNameLabel.textAlignment = UITextAlignmentLeft;
            searchNameLabel.font = [UIFont systemFontOfSize:14.0f];
            searchNameLabel.tag=260;
            [cell.contentView addSubview:searchNameLabel];
            [searchNameLabel release];

            searchSugarLabel= [[UILabel alloc] initWithFrame:frame];
            searchSugarLabel.textColor = [UIColor blackColor];
            searchSugarLabel.backgroundColor = [UIColor clearColor];
            searchSugarLabel.textAlignment = UITextAlignmentLeft;
            searchSugarLabel.font = [UIFont systemFontOfSize:14.0f];
            searchSugarLabel.tag=160;
            [searchSugarLabel setHidden:YES];
            [cell.contentView addSubview:searchSugarLabel];
            [searchSugarLabel release];
        }
        else{       
        nameLabel = [[UILabel alloc] initWithFrame:frame];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.textAlignment = UITextAlignmentLeft;
        nameLabel.font = [UIFont systemFontOfSize:14.0f];
        nameLabel.tag=60;
        [cell.contentView addSubview:nameLabel];
        [nameLabel release];

        sugarLabel= [[UILabel alloc] initWithFrame:frame];
        sugarLabel.textColor = [UIColor blackColor];
        sugarLabel.backgroundColor = [UIColor clearColor];
        sugarLabel.textAlignment = UITextAlignmentLeft;
        sugarLabel.font = [UIFont systemFontOfSize:14.0f];
        sugarLabel.tag=160;
        [sugarLabel setHidden:YES];
        [cell.contentView addSubview:sugarLabel];
        [sugarLabel release];
        }
    }
    else {        

        if(isSearchOn)
        {
            searchNameLabel=(UILabel *)[cell.contentView viewWithTag:260];
            searchSugarLabel=(UILabel *)[cell.contentView viewWithTag:160];
        }
        else{
            nameLabel=(UILabel *)[cell.contentView viewWithTag:60];
            sugarLabel=(UILabel *)[cell.contentView viewWithTag:160];
        }

    }


    if (isSearchOn) { 

        cellValue = [searchResult objectAtIndex:indexPath.row]; 
        searchSugarLabel.text=cellValue.sugarId;
        NSString *searchText = [NSString stringWithFormat:@"%@ %@", cellValue.firstName, cellValue.lastName];
        searchNameLabel.text=searchText;
        NSLog(@"%@",searchNameLabel.text);
        NSLog(@"%@",searchSugarLabel.text);
    }


     else {

            NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
            NSArray *contactSection=[contactNames objectForKey:contact];
            sugar=[db getSugarId:@"Contacts" bySection:contact andIndex:indexPath.row];


            NSString *cellText = [contactSection objectAtIndex:[indexPath row]];

            // split the text by the : to get an array containing { "AAA", "BBB" }
            NSArray *splitText = [cellText componentsSeparatedByString:@":"];

            // form a new string of the form "BBB AAA" by using the individual entries in the array
            NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
            nameLabel.text = contactText;
            sugarLabel.text = sugar;
}
    return cell;
}

Contacts 是一个类,其中包含属性 firstName、lastName 和 Sugar id。我将联系人类的属性分配给数据库方法中的变量并返回联系人对象数组。 searchResult 现在是一个联系人对象数组。问题是当我在控制台上记录内容时,数据库会获取其中的所有内容并返回一个联系人数组。searchResult 中的联系人指向不同的内存位置但是当我尝试调试cellForRowAtIndexPath 方法在获得 6 个联系人后。第 7 个联系人指向与第一个联系人相同的内存位置,因此在 searchNameLabel.text 中重复它返回 null 并且 indexPath 指向 nil ......我认为这是单元格重用问题我承认我在这方面很糟糕..我需要弄清楚这一点,因为我将用这个来完成我的项目......请大家帮助我......

仅当我尝试搜索联系人时才会发生这种情况。当我尝试将所有联系人加载到桌子上时它工作正常..

【问题讨论】:

  • 你能具体说明你编辑了什么内容吗?
  • 只有标签:objective 和 c to objective-c。点击上方“已编辑”后的链接即可查看修订历史。
  • 哦,谢谢..你能告诉我可能是什么问题吗??
  • 要查看编辑中发生的变化,只需点击时间戳

标签: iphone objective-c uitableview memory-management


【解决方案1】:

您在if(cell== nil) 块中初始化子视图,但在相应的else-块中,您再次覆盖它们。

你应该重新考虑你的设计:不要通过searchon加载不同的视图,而是根据searchon设置它们的属性

if(cell == nil){
   //do all initializing
}

if(searchon){
    //set view/label properties for searching style
} else {
    //set view/label properties for not-searching style
}

另一种方法是为 searchon/!searchon 完全分离 NIB 文件

if(searchon){
    static NSString *SearchOnCellIdentifier = @"SearchOnCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SearchOnCellIdentifier];
    if (cell == nil){
        //load cell from extra nib
    }

} else {
    static NSString *SearchOFFCellIdentifier = @"SearchOFFCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SearchOFFCellIdentifier];
    if (cell == nil){
        //load cell from extra nib
    }
}

注意:我从来没有这样做过,也没有经过测试。

【讨论】:

  • 感谢 viking..我采用了两个不同的单元格标识符..所以如果 searchIsOn 它指向一个标识符,否则它指向另一个标识符..解决了问题...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多