自定义UITableViewCell与新关联的.xib单元格冲突

时间:2014-07-15 20:43:38

标签: ios objective-c xcode uitableview

我已经实现了“清除”应用程序样式滑动以删除和存档文章(SHCTableViewCell)。它运作良好。现在我想在我的单元格上有更多的标签,我已经创建了自定义表格视图单元格形式的xib文件。然后,我将它与我的自定义单元格(使用滑动)的类相关联。我给xib cell提供了一个标识符。

并在我的cellForRowAtIndexPath中添加了此代码:

if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"SHCTableViewCell_inbox" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }
好的,好的!现在我可以看到我的自定义标签,但滑动不起作用。 什么可能导致这场冲突?

InboxViewController.m

. . . 

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

    static NSString *CellIdentifier = @"ContentCell";

    SHCTableViewCell_inbox *cell = nil;//[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"SHCTableViewCell_inbox" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }
    cell.textLabel.backgroundColor = [UIColor clearColor];
    Article* article = [self.articles objectAtIndex:indexPath.row];
    cell.blog.text = article.blog;
    cell.rating.text = [NSString stringWithFormat: @"%ld", (long)article.rating];
    cell.title.text = article.title;
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    cell.delegate = self;
    cell.todoItem = article;
    return cell;
}

. . .

xib单元格文件:

enter image description here

编辑: 我认为这与手势识别器有关。 SHCTableViewCell_inbox有initWithStyle:显然,它永远不会被调用。 下一行代码在initWithStyle中,负责手势识别。     UIGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan :)];             recognizer.delegate = self;             [self addGestureRecognizer:Recognizer];

1 个答案:

答案 0 :(得分:1)

正如我们在聊天中所发现的那样:在故事板中的单元初始化的代码位于initWithStyle:reuseIdentifier:方法中。在重构为Xib之后,您应该将该代码移至awakeFromNib方法,因为initWithStyle:reuseIdentifier:不会被调用。

所以你应该在SHCTableViewCell_inbox类下面添加代码:

- (void)awakeFromNib {
    UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    recognizer.delegate = self;
    [self addGestureRecognizer:recognizer];

    // any other code that was in initWithStyle:reuseIdentifier:
}
相关问题