【发布时间】:2019-11-11 09:42:23
【问题描述】:
我的代码是这样的
viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] init];
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = @"Hello, world";
cell.delegate = self;
return cell;
}
细胞.m
- (void)awakeFromNib {
[super awakeFromNib];
[self commonInit];
// Initialization code
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
[self addGestureRecognizer:longPress];
}
- (void)longPress
{
[self.delegate actionTriggered:self];
}
所以屏幕显示了一个只有一个单元格包含“hello world”的表格视图 使用长按手势时没有任何反应。
所以我在 commonInit 中设置了一个断点。 找出这个方法没有被调用。 不知道为什么。以及如何解决它。 感谢任何帮助。
【问题讨论】:
-
既然你注册了一个类,它应该调用`initWithStyle:reuseIdentifier:`。参照。讨论developer.apple.com/documentation/uikit/uitableview/…
标签: ios objective-c uitableview