在uitableview中更改标签

时间:2010-08-10 12:46:22

标签: iphone uitableview

我正在使用表格视图来编辑对象。

我使用下面的代码构建一个uitableview单元格。

它工作得很好,但不知何故滚动细胞在内部发生变化。

static NSString *CellIdentifier2 = @"CustomCell Editable Identifier";

        cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {

            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                           reuseIdentifier:CellIdentifier2] autorelease];


            cell.textLabel.text = rowLabel;

            UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 170, 25)];

            theTextField.textAlignment = UITextAlignmentRight;

            cell.editingAccessoryView =theTextField;
            theTextField.tag = 602; 
            if ([rowKey isEqualToString:@"phone_no"]) {
                [theTextField setKeyboardType:UIKeyboardTypeNumberPad];
            }
            theTextField.text = [rowValue ddEventValueDisplay];
            [theTextField release];
            return cell;

谁能告诉我哪里错了?

3 个答案:

答案 0 :(得分:1)

当单元格为NIL时,您正在创建标签。因此,每次滚动时,都会调用cellForRowAtIndexPath并检查单元格是否为NIL(这不是时间),因此在if(cell == NILL)块中没有执行任何操作。

因此,要设置标签文本,您需要从单元格视图中标出此标签,然后将文本设置为:

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

a = [[UILabelView alloc] init];
a.tag = 10;
[cell.contentView addSubview:a];

}

UILabelView * aView = [cell.contentView viewWithTag:10];

aView.text = @“有些文字”;

[cell.contentView addSubview:a];

 return cell;

}

答案 1 :(得分:1)

上面的代码在你的cellForRowAtIndexPath调用中,对吗?

我不确定这是否是复制和粘贴故障的结果,但你想在initWithStyle调用之后立即包装“if(cell == nil)”块,在上面的代码中你错过了结束花括号。

答案 2 :(得分:0)

看起来您只在创建时配置单元格。但是当你从

获得可重复使用的细胞时
cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier2];

你没有改变它 - 但你应该,它们是随机的:例如你想要行= 1的单元格并从行= 3

获得重用的单元格 顺便说一下:总是更好地提供完整的代码 - 你不应该强迫人们猜测你的意思。