ios - 自定义 TableViewCell 与新关联的 .xib 单元冲突

标签 ios objective-c xcode uitableview

我已经实现了用于删除和归档文章的“清除”应用样式滑动 (SHCTableViewCell)。它运作良好。现在我想在我的单元格上添加更多标签,并且我已经创建了自定义表格 View 单元格表单 xib 文件。然后,我将它与我的自定义单元格(使用滑动)的类相关联。我给 xib 单元格一个标识符。

并在我的 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; [自行添加手势识别器:识别器];

最佳答案

正如我们在聊天中发现的那样:在 Storyboard 中时初始化单元格的代码驻留在 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:
}

关于ios - 自定义 TableViewCell 与新关联的 .xib 单元冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24767824/

相关文章:

iphone - 调用 rootViewController 在内容 View 中切换 View (iOS)

ios - 如何更改 pushViewController 动画持续时间?

ios - 通过从应用程序商店下载的同一个应用程序从 Xcode 安装 iOS 应用程序(反之亦然)

ios - Xcode 图像切片 - 拉伸(stretch)图像的外部但保留中心区域

ios - 在 UIScrollView 中居中 UIButtons

ios - Swift 中的 Int 与 Integer

ios - 当 iPad 进入分屏多任务模式时收到通知

ios - 如何使用 UILabel 或 UITextview 实现文本的 LineSpacing 和背景颜色

objective-c - double 目标数组 c

ios - Cordova 5 构建命令正在删除 iOS 设备方向设置