簡體   English   中英

如何防止UITableViewCell被選擇?

[英]How do I prevent a UITableViewCell from being selectable?

我從該靜態UITableViewCell的情節提要場景中選擇了Selection = None 我還添加了此方法:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
            return nil;
            break;
    }
}

這樣做的設置不是特別好,因為它會警告方法不返回值。

該單元仍然可以選擇,一次,短暫,然后不再。 像這樣設置單元:

case 1:
    cell.textLabel.text = @"Search";
    break;

didSelectRowAtIndexPath這樣:

case 1:
    NSLog(@"Unselectable Cell");
    break;

我還有什么想念的嗎?

cell.userInteractionEnabled = NO;

選擇是突出顯示后的狀態,如果您按一個單元格並且它變成藍色,則該狀態會被突出顯示,釋放它時,它已被選中。

因此,如果您無法突出顯示該單元格,則無法選擇它。 考慮到這一點,您可以執行以下操作:

- (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;
}

我要做的就是這樣:

- (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;
}

您可以逐行進行,當然,這僅適用於不會動態更改單元格數量的表。

您也可以嘗試

tableView.allowsSelection = NO;

另一種方式

cell.selectionStyle = UITableViewCellSelectionStyleNone;

多一個

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM