我已经实现了用于删除和归档文章的“清除”应用样式滑动 (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 单元格文件:
编辑: 我认为这与手势识别器有关。 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/