【问题标题】:UITableview cell value over writing again and again problem?UITableview 单元格值一遍又一遍的写入问题?
【发布时间】:2009-10-31 04:30:30
【问题描述】:

当我滚动时,我的 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;

}

【问题讨论】:

  • 如果您发布用于将内容放入单元格的代码,我们会更容易为您提供帮助。例如,-tableView:cellForRowAtIndexPath: 方法中的代码。

标签: iphone


【解决方案1】:

出队的单元格已经有一个标签,因此您要向这些单元格添加另一个标签。在初始 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;
}

【讨论】:

    【解决方案2】:

    在我看来,问题是每次表格视图滚动时您都在重新分配一个新标签,这导致它被重新写入单元格。

    还要确保您正确使用dequeueReusableCellWithIdentifier。可能是您没有正确地将其出队。

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

    【讨论】:

    • 问题是每次滚动时您都在重新分配标签。您可以在 viewDidLoad 中分配标签,然后在 cellForRowAtIndexPath 中设置文本。
    • 作为一项规则,始终确保在 cellForRowAtIndexPath 中没有“alloc”调用。否则每次滚动表格视图时都会调用它,并会导致一次又一次地覆盖相同的标签。
    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2017-10-10
    • 1970-01-01
    相关资源
    最近更新 更多