UITableview单元格值反复写入一次又一次的问题?

时间:2009-10-31 04:30:31

标签: iphone

当我滚动时,我的tableview显示就像这样......你会帮助我做错了吗?请问有什么帮助吗?如果你看到那张图片,我选择的价值超过了写作........

http://www.freeimagehosting.net/image.php?fa76ce6c3b.jpg

代码是

  • (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];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}




CGRect rectLbl2 = CGRectMake(75 ,1, 215, 32);
CGRect rectLbl3 = CGRectMake(75 ,28, 215, 30);
addLabel2= [[UILabel alloc] initWithFrame:rectLbl2];
addLabel3= [[UILabel alloc] initWithFrame:rectLbl3];



addLabel2.text = @"senthil";
[cell addSubview:addLabel2];
[addLabel2 release]; // for memory release


addLabel3.text= @"senthil";
[cell addSubview:addLabel3];
[addLabel3 release];


return cell;

}

2 个答案:

答案 0 :(得分:5)

出列的单元格已经有标签,因此您要为这些单元格添加另一个标签。在初始UITableViewCell创建中添加标签,只需更改标签内容。

尝试这样的事情:

(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];
            cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        UILabel *lab;
        CGRect rectLbl2 = CGRectMake(75 ,1, 215, 32);
        lab = [[UILabel alloc] initWithFrame:rectLbl2];
        lab.tag = 2;
        [cell addSubview:lab];
        [lab release];

        CGRect rectLbl3 = CGRectMake(75 ,28, 215, 30);
        lab = [[UILabel alloc] initWithFrame:rectLbl3];
        lab.tag = 3;
        [cell addSubview:lab];
        [lab release];
    }

    ((UILabel *)[cell viewWithTag:2]).text = @"senthil";
    ((UILabel *)[cell viewWithTag:3]).text = @"senthil";

    return cell;
}

答案 1 :(得分:0)

我认为问题在于,每次tableview滚动时都会重新分配一个新标签,这会导致它重新写入单元格。

还要确保正确使用dequeueReusableCellWithIdentifier。可能是您没有正确出列的情况。

这就是我可以说的所有内容,而无需查看您的代码。