【问题标题】:dequeueReusableCellWithIdentifier: always returns 'nil' for visible cellsdequeueReusableCellWithIdentifier:总是为可见单元返回“nil”
【发布时间】:2011-09-27 10:36:07
【问题描述】:

我创建了一个自定义表格视图单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITextField *editField=nil;
...

NSString *CellIdentifier = [NSString  stringWithFormat:@"cell:%d",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Configure the cell...
    switch (indexPath.row) {

        case 0: 
        {

            cell.textLabel.text=DEVNAME_TEXT_NDVC;
            cell.textLabel.font=[UIFont boldSystemFontOfSize:LABEL_TEXTSIZE_NDVC];

            editField=[[UITextField alloc] initWithFrame:CGRectMake(158, 9, cell.frame.size.width-183, cell.frame.size.height-15) ];
            editField.tag=DEVNAME_TAG_NDVC;
            ...
            [cell.contentView addSubview:editField ];
            [editField release];

        }
        break;

表格只有 5 行,每行始终显示在屏幕上。 后来,当我尝试访问单元格时,我总是得到'nil'

当用户点击单元格时,以下代码应该将光标放置在适当的 UITextField 上,但它没有,因为“单元格”总是 =0。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *CellIdentifier = [NSString stringWithFormat:@"cell:%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *tf=nil;

[tableView deselectRowAtIndexPath: indexPath animated: YES];
[activeField resignFirstResponder]; // Last used UITextField

switch (indexPath.row) {
    case 0: //
        tf=(UITextField*)[cell.contentView viewWithTag:DEVNAME_TAG_NDVC];
        [tf becomeFirstResponder]; // Show the keyboard
        //[tf performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.7];
    break;

拜托,你能提出什么问题吗?为什么[tableView dequeueReusableCellWithIdentifier:CellIdentifier]总是=0, 但所有表格单元格始终可见。

谢谢。

【问题讨论】:

  • 我也有一个问题:[tableView dequeueReusableCellWithIdentifier:CellIdentifier] 在某些按钮点击时总是=0

标签: iphone uitableview


【解决方案1】:

也许我不明白这个问题,但表格单元格不是只有在不再显示后才可重复使用吗?如果它们仍然可见,你怎么能重复使用它们?

【讨论】:

    【解决方案2】:

    改变这个:

    NSString *CellIdentifier = [NSString  stringWithFormat:@"cell:%d",indexPath.row];
    

    到:

    static NSString *CellIdentifier = @“XXXX”;
    

    【讨论】:

      【解决方案3】:

      是的,dequeueReusableCellWithIdentifier: 总是返回 nil 除了使用 registerNib:forCellReuseIdentifier:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        • 2012-11-02
        • 2012-04-17
        • 2015-03-06
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        相关资源
        最近更新 更多