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