Dex*_*erW 35 iphone cocoa-touch uitableview ios
如何禁用在UITableView中仅选择单个单元格?我有几个,我只希望最后一个被禁用.
Cam*_*hez 52
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = ...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*phH 52
要停止选择某些单元格,请使用:
cell.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)
除了阻止选择之外,这还会阻止tableView:didSelectRowAtIndexPath:为设置它的单元格调用.它也会使画外音像暗灰色按钮一样(可能是你想要的也可能不是).
请注意,如果单元格中有交互元素(即开关/按钮),则需要使用cell.selectionStyle = UITableViewCellSelectionStyleNone;,然后确保忽略单元格中的点击tableView:didSelectRowAtIndexPath:.
小智 19
把它放在你的自定义表VC中:
// cells lacking UITableViewCellAccessoryDisclosureIndicator will not be selectable
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) {
return nil;
}
return indexPath;
}
// disabled cells will still have userinteraction enabled for their subviews
- (void)setEnabled:(BOOL)enabled forTableViewCell:(UITableViewCell *)tableViewCell
{
tableViewCell.accessoryType = (enabled) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
// if you dont want the blue selection on tap, comment out the following line
tableViewCell.selectionStyle = (enabled) ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
}
Run Code Online (Sandbox Code Playgroud)
然后启用/禁用someTableViewCell的选择,执行以下操作:
[self setEnabled:state forTableViewCell:someTableViewCell];
Run Code Online (Sandbox Code Playgroud)
你完成了,可以发货.
Aar*_*ers 10
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self numberOfRowsInSection] == [indexPath row]) {
return nil;
} else {
return indexPath;
}
}
Run Code Online (Sandbox Code Playgroud)
不会选择表格的最后一行
| 归档时间: |
|
| 查看次数: |
35389 次 |
| 最近记录: |