[英]How do I prevent a UITableViewCell from being selectable?
I have selected Selection = None
from the storyboard scene of that static UITableViewCell
. 我从该静态UITableViewCell
的情节提要场景中选择了Selection = None
。 I also added this method: 我还添加了此方法:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 1:
return nil;
break;
}
}
Which doesn't particularly set well because it warns of a method not returning a value. 这样做的设置不是特别好,因为它会警告方法不返回值。
The cell is still selectable, one time, briefly and then no more. 该单元仍然可以选择,一次,短暂,然后不再。 The cell is set up like this: 像这样设置单元:
case 1:
cell.textLabel.text = @"Search";
break;
and the didSelectRowAtIndexPath
like so: 和didSelectRowAtIndexPath
这样:
case 1:
NSLog(@"Unselectable Cell");
break;
Am I missing anything else? 我还有什么想念的吗?
cell.userInteractionEnabled = NO;
Selection is the state right after being highlighted, if you press on a cell and it turns blue, it's being highlighted, when you release it, it has been selected. 选择是突出显示后的状态,如果您按一个单元格并且它变成蓝色,则该状态会被突出显示,释放它时,它已被选中。
Therefore, if you can't highlight the cell, you can't select it. 因此,如果您无法突出显示该单元格,则无法选择它。 With that in mind, you can do: 考虑到这一点,您可以执行以下操作:
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// I'm assuming you just want it for the row 1, right?
if (indexPath.row == 1)
{
// You ain't getting selected today, son.
return NO;
}
return YES;
}
What I do is I do something like so: 我要做的就是这样:
- (UITableViewCell *)tableView:(UITableView *)tmpTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
tmpTableView.scrollEnabled = NO;
static NSString *CellIdentifier = @"MainCell";
UITableViewCell *cell = [tmpTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(indexPath.row == 0)
{
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.userInteractionEnabled = NO;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 4;
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.font = [UIFont systemFontOfSize:16.0];
cell.textLabel.text = [self.answerArray objectAtIndex:indexPath.row];
[cell.textLabel setAdjustsFontSizeToFitWidth:YES];
return cell;
}
}
cell.textLabel.font = [UIFont systemFontOfSize:18.0];
cell.textLabel.text = [self.answerArray objectAtIndex:indexPath.row];
return cell;
}
You could go line by line, of course this would only work for tables that dont dynamically change in number of cells. 您可以逐行进行,当然,这仅适用于不会动态更改单元格数量的表。
You can also try this 您也可以尝试
tableView.allowsSelection = NO;
another way of doing this 另一种方式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
one more 多一个
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.