如何从 UIButton 操作方法获取 UITableViewCell 对象? [英] How can I get UITableViewCell object from a UIButton action method?
问题描述
我有一个 UITableView,其中我为每个单元格添加了一个 UIButton 作为附件视图.请注意,我将按钮的标签设置为当前行以备将来使用.
I have a UITableView in which I have added a UIButton as accessory view for each cell. Note that I set the tag of the button as current row for future use.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.frame = CGRectMake(0, 0, 30, 30);
[cellButton setBackgroundImage:[UIImage imageNamed:@"cellButton.png"] forState:UIControlStateNormal];
[cellButton addTarget:self action:@selector(cellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
cellButton.tag = indexPath.row; // <= Will use this in the next method
cell.accessoryView = cellButton;
}
//Load cell with row based data
return cell;
}
现在,当点击这些按钮之一时,我需要对单元格进行更改.所以我实现了 cellButtonAction ,在那里我使用标签来取回单元格:
Now when one of these buttons is tapped, I need to make changes to the cell. So I implement cellButtonAction where I use the tag to get back the cell:
-(void)editCommentButtonAction:(id)sender
{
UIButton *button = sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self makeChangesToCell:cell];
}
但这似乎是一种非常迂回的方式.有没有更干净的方法来做到这一点?
But this seems like a very round about way. Is there a cleaner way to do this?
推荐答案
所以假设按钮直接在contentView
中:
So assuming that the button is in the contentView
directly:
询问
sender
"(即按钮)的superview,即cell的contentView
ask "
sender
" (ie the button) for its superview, which is the cell's contentView
请求该视图的superView
,即单元格
ask that view for its superView
, which is the cell
向 tabview 询问这个单元格的索引:
ask the tabview for the index of this cell:
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
实际上,我现在使用一种通用方法或函数,它只是向上走超级视图,寻找KindOf"、UITableViewCell 或 UICollectionViewCell 的视图.像冠军一样工作!
Actually, I use a general purpose method or function now that just walks up the superviews, looking for a view that is 'KindOf' a UITableViewCell or a UICollectionViewCell. Works like a champ!
Swift 中的代码:
The code in Swift:
func containingUITableViewCell(tableView: UITableView, var view: UIView) -> (UITableViewCell, NSIndexPath)? {
while let v = view.superview {
view = v
if view.isKindOfClass(UITableViewCell.self) {
if let cell = view as? UITableViewCell, let indexPath = tableView.indexPathForCell(cell) {
return (cell, indexPath)
} else {
return nil
}
}
}
return nil
}
func containingUICollectionViewCell(collectionView: UICollectionView, var view: UIView) -> (UICollectionViewCell, NSIndexPath)? {
while let v = view.superview {
view = v
if view.isKindOfClass(UICollectionViewCell.self) {
if let cell = view as? UICollectionViewCell, let indexPath = collectionView.indexPathForCell(cell) {
return (cell, indexPath)
} else {
return nil
}
}
}
return nil
}
这篇关于如何从 UIButton 操作方法获取 UITableViewCell 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!