- 效果如圖所示
選中某個cell.gif
-
應用場景
- 選中cell上面的某一個button時其他cell上buttonw
- 選中這個cell在cell的右端有個標識代表選中
-
實現思路
- 記錄下當前點擊button所在cell的IndexPath.row
- 在數據源方法中判斷記錄下的IndexPath.row是否等于當前的IndexPath.row
- 如果相等設置cell上button為選中狀態即可
-
代碼如下
- 直接刷新Tableview
- (void)selectedBtnClick:(UIButton *)button
{
// 通過button計算出其所在的cell
UITableViewCell * cell = (UITableViewCell *)[[button superview] superview];
NSIndexPath * path = [self.hsTabbleView indexPathForCell:cell];
// 記錄下當前的IndexPath.row
self.indexPathRow = path.row;
// 刷新數據源方法
[self.hsTabbleView reloadData];
}
- 僅刷新Button所在Section的cell(更節省資源)
- (void)selectedBtnClick:(UIButton *)button
{
// 通過button計算出其所在的Cell
UITableViewCell * cell = (UITableViewCell *)[[button superview] superview];
NSIndexPath * path = [self.hsTabbleView indexPathForCell:cell];
// 記錄下當前的IndexPath.row
self.indexPathRow = path.row;
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:path.section];
// 刷新數據源方法
[self.hsTabbleView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
}
- 數據源方法中
if (self.indexPathRow == indexPath.row) {
// 如果是當前cell
cell.supportBtn.selected = YES;
}else{
cell.supportBtn.selected = NO;
}