简体   繁体   English

如何防止UITableViewCell被选择?

[英]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.

相关问题 如何防止用户将UITableViewCell移动到特定位置? - How do I prevent the user from moving a UITableViewCell to a specific location? 如何防止UITableViewCell从其自己的部分移开? - How to prevent a UITableViewCell being moved away from its own section? 暂时阻止UITableViewCell被回收 - temporarily prevent a UITableViewCell from being recycled 使用插入动画时,如何防止UITableViewCell的内容与其他单元格重叠? - How do I prevent my UITableViewCell's contents from overlapping other cells when using insert animation? 在 Swift 中,如何防止函数在子类上被调用? - In Swift, how do I prevent a function from being called on a subclass? 如何从UITableViewCell获取信息? - How do I get information from a UITableViewCell? 我在自定义UITableViewCell类中覆盖了setHighlight。 但是,我并不总是想要使用它。 如何防止其被使用? - I've overrode setHighlight in my custom UITableViewCell class. However, I don't always want to use it. How can I prevent from it being used? 如何在UITableViewCell中获得UITableViewCell? - How do I get a UITableViewCell in a UITableViewCell? 防止将UIView重新添加到UITableViewCell - Prevent UIView's from being re-added to a UITableViewCell 当从UIBarButtonItem呈现方向更改后,如何防止UIPopoverController passthroughViews被重置? - How do I prevent UIPopoverController passthroughViews from being reset after orientation change when presented from a UIBarButtonItem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM