Ios 当UITableView行不是UITableViewCell时,如何拦截该行上的点击?
我以前是这样工作的:Ios 当UITableView行不是UITableViewCell时,如何拦截该行上的点击?,ios,Ios,我以前是这样工作的: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; } - (void)t
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
但是现在我必须在一个不是UITableViewCell的单元格上做同样的事情:
我必须在单元格中放很多东西,所以我不能使用普通的UITableViewCells,我自己做了一个(称为PrototypeTableViewCell)。像这样:
@interface PrototypeTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *eventTitle;
@property (weak, nonatomic) IBOutlet UILabel *eventLocation;
@property (weak, nonatomic) IBOutlet UILabel *eventStartDate;
@property (weak, nonatomic) IBOutlet UILabel *eventEndDate;
@end
那么,我该如何在普通的TableViewCells上做我以前做的事情(比如设置附件:复选标记)?
“cellForRowAtIndexPath:indexPath”不能与我创建的类一起使用。您的原型TableViewCell是UITableViewCell的子类。只需将UITableViewCell强制转换为PrototypeTableViewCell,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// cast UITableViewCell to your PrototypeTableViewCell
PrototypeTableViewCell *cell = (PrototypeTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
// use its properties as normal
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
PrototypeTableViewCell是UITableViewCell的子类。只需将UITableViewCell强制转换为PrototypeTableViewCell,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// cast UITableViewCell to your PrototypeTableViewCell
PrototypeTableViewCell *cell = (PrototypeTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
// use its properties as normal
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
@只是为了补充(只提供代码而不解释是不好的)。您的单元格不是“原型”,而是
UITableViewCell
的子类。它仍然是一个UITableViewCell
您刚刚添加了一些自定义内容。所以这个函数仍然可以工作,你只需要将它转换到你的自定义子类中。对此很抱歉。对上面的代码示例进行了编辑,只是为了添加(只提供代码而不进行解释是不好的)。您的单元格不是“原型”,而是UITableViewCell
的子类。它仍然是一个UITableViewCell
您刚刚添加了一些自定义内容。所以这个函数仍然可以工作,你只需要将它转换到你的自定义子类中。对此很抱歉。对上面的代码示例进行了编辑。