【问题标题】:uitextfield of custom table cell自定义表格单元格的uitextfield
【发布时间】:2013-01-29 11:58:14
【问题描述】:

我正在创建一个包含三种不同类型的自定义单元格的表格,其中一个自定义单元格具有UITextField。我使用这个自定义单元格创建了两行。我为两行的文本字段设置标签和委托。 我的问题是,当我滚动表格时,这些带有文本字段的行向上移动并从屏幕上消失,向下滚动时,我的应用程序崩溃了。 我收到类似的错误

-[CellImageViewController txtField]: unrecognized selector sent to instance 0xa0ea5e0

这是我的cellForRowAtIndexPath:代码

if (indexPath.row == 0 )
        {
            if (cell == nil) {
                cell = [[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
            }

            cell.txtField.tag =1;

            cell.txtField.delegate = self;
            cell.txtField.text = @"kjfkd";
            }


            return cell;

        }
        else if(indexPath.row==1)
        {
            if (cell == nil) {
                cell = [[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
            }

           cell.txtField.tag = 2;
           cell.txtField.text = @"glk";
           cell.txtField.delegate = self;

          return cell;
     }

有人知道这个问题吗?

【问题讨论】:

    标签: iphone ios uitableview uitextfield custom-cell


    【解决方案1】:

    您有不同类型的单元格,因此您需要为每个单元格使用不同的单元格标识符。

    试试这个:

    customOrNormalCell *cell [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
    if(!cell){
        cell = [[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%i", indexPath.row]];
    }
    

    使用此代码,每个 cellIdentifier 将是 1,2,3,4...(都不同)

    我很确定它会解决问题

    【讨论】:

      【解决方案2】:

      在创建 UITableView 之前创建 UITextField 并在单元格内容视图中添加您的 Textfield 对象

      [cell.contentView addSubview:yourTxtFieldOblect];
      

      【讨论】:

      • 是的.. 我这样做了但仍然崩溃
      • 创建一个自定义方法并在创建UITableView之前放置UITextField的初始化代码和该方法调用(只放置初始化代码不添加)并将其添加到单元格内容视图...试试看..可能会解决你的问题:)
      【解决方案3】:

      看起来您可能正在重用不同类型的单元格。 在尝试访问它的属性之前,请尝试确保您正在重用的单元格是哪种类。

      【讨论】:

        【解决方案4】:

        为什么要为每一行定义单元格?

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
         NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
         CellWithTextFieldViewController  *cell = (CellWithTextFieldViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        
          if(cell == nil)
          {
            cell =[[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
        
            cell.txtField.tag = indexPath.row;
            cell.txtField.text = [SET TEXT FROM ARRAY];
            cell.txtField.delegate = self;
          }
        }
        

        【讨论】:

          【解决方案5】:

          我的项目中也遇到过这个问题。

          试试这个:

          禁用 Tableview 中的滚动属性并创建一个滚动视图。然后在滚动视图中添加您的 Tableview。

          UITableView  *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 976, 395) style:UITableViewStylePlain];
          [table setSeparatorStyle:UITableViewCellSelectionStyleNone];
          UIScroll *scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(24, 168, 978, 395)];
           [table setScrollEnabled:NO];
          
          [scroll addSubview:table];
          [self.view addSubview:scroll];
          [scroll sendSubviewToBack:table];
          

          【讨论】:

            【解决方案6】:

            试试这个

                 static NSString *cellIdentifier= @"Cell";
            
                CellWithTextFieldViewController *cell = (CellWithTextFieldViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            
                    if (cell == nil) {
                        cell = [[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                    }
            
                    cell.txtField.tag =indexPath.row+1;
            
                    cell.txtField.delegate = self;
                    cell.txtField.text = @"kjfkd";
            
                    return cell;
            

            【讨论】:

              【解决方案7】:

              在滚动 tableView 时,您的单元格会被重用。这就是为什么 textField 正在消失。尝试对不同的自定义单元格使用不同的单元格 ID。不要忘记在 nib 文件中为单元标识符提供相同的 id

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

              {

              static NSString *customCellOne = @"customCell1";
              static NSString *customCellTwo = @"customCell2";
              UITableViewCell *cell =nil;
              
              
              
              if (0 == indexPath.row)
              {
                  cell = (CustomCellOne *)[tableView dequeueReusableCellWithIdentifier:customCellOne];
              
                  if (nil == cell)
                  {
                      // Create custom cell one and do what ever you want
              
                  }
              }
              else if (1 == indexPath.row)
              {
                  cell=(CustomCellTwo *)[tableView dequeueReusableCellWithIdentifier:customCellTwo];
              
                  if (nil == cell)
                  {
                       // Create custom cell two and do what ever you want       
                  }
              
              }
              
              return cell;
              

              }

              【讨论】:

                【解决方案8】:

                我不知道,可能是在重用单元格时,文本字段委托被释放。 也许你可以设置

                  textfield.deleagate = self;
                

                在 CellWithTextFieldViewController.m 中

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-07-10
                  • 2014-08-23
                  相关资源
                  最近更新 更多