【问题标题】:How to initialize a custom UITableViewCell from didSelectRowAtIndexPath如何从 didSelectRowAtIndexPath 初始化自定义 UITableViewCell
【发布时间】:2014-02-01 01:13:19
【问题描述】:

我想知道如何获取我的自定义 UITableViewCell 的实例,以便我可以将 UITextfield 设置为第一响应者

这就是我在 cellForRowAtIndexPath 中设置自定义单元格的方式

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

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomFinishingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

但是,当我尝试在 didSelectRowAtIndexPath 中设置所选单元格的实例时,出现错误

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomFinishingCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // error
    [cell.widthTexField becomeFirstResponder];

这是我收到的错误

使用“UITableViewCell *”类型的表达式初始化“CustomFinishingCell *”的不兼容指针类型

【问题讨论】:

  • 编译器错误,还是运行时错误?

标签: ios uitableview uitextfield


【解决方案1】:

您忘记将cellForRowAtIndexPath: 返回的UITableViewCell 转换为您的子类

CustomFinishingCell *cell = (CustomFinishingCell *)[tableView cellForRowAtIndexPath:indexPath]; // error

此外,我不知道您是否只是从您发布的代码中省略了这一点,但您实际上从未在 cellForRowAtIndexPath: 中声明单元格

CustomFinishingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

【讨论】:

  • 关于你的第二行代码,我认为这行代码 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 本质上是在做同样的事情......你能详细说明一下吗在那?只是我在选择视图之外的 nextUITableViewCell 时遇到了问题,它没有实例化自己。所以我遇到了错误,我只是不知道为什么。
【解决方案2】:

尝试添加演员表:

CustomFinishingCell *cell = (CustomFinishingCell*)[tableView cellForRowAtIndexPath:indexPath]; 

您应该会在此行中看到一条警告,说明做作存在问题。

【讨论】:

    【解决方案3】:
    1. initWithStyleUITableViewCell的初始化方法,你有没有重写它并返回CustomFinishingCell

    2. 添加演员表

      CustomFinishingCell *cell = (CustomFinishingCell*)[tableView cellForRowAtIndexPath:indexPath]; 
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多