如何从didSelectRowAtIndexPath初始化自定义UITableViewCell

时间:2014-02-01 01:13:19

标签: ios uitableview uitextfield

我想知道如何获取我的自定义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 *”的指针类型不兼容

3 个答案:

答案 0 :(得分:3)

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

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

此外,我不知道你是否只是从你发布的代码中省略了这一点,但是你从未真正在cellForRowAtIndexPath:

中声明了这个单元格。
CustomFinishingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

答案 1 :(得分:0)

尝试添加演员:

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

你应该在这一行看到一个警告,说明在做作中存在问题。

答案 2 :(得分:0)

  1. initWithStyleUITableViewCell的初始化方法,您是否覆盖它并返回CustomFinishingCell

  2. 添加演员

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