禁用 TableView 单元格

Map*_*Dev 1 uitableview ios

如何禁用 UITableView 中除前 3 个单元格之外的所有单元格?

如果选择了禁用的单元格之一,则返回 nil:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if ( indexPath.row >= 3 ) {
    [[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]] setAlpha:0.5];
    return nil;
}

return indexPath; }
Run Code Online (Sandbox Code Playgroud)

如何使禁用的单元格“可见”? 这有效,但我只在启动时,因为每个单元格的索引路径在滚动时发生变化(重用单元格):

for(int i = 3; i <= 100; i++) {
    [[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] setAlpha:0.5];
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*ain 5

您可以通过设置selectionStyle属性来禁用单元格,请参见下面的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = ...

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
Run Code Online (Sandbox Code Playgroud)

禁用单元格选择的另一种方法是禁用单元格的用户交互。请参阅下面的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UITableViewCell *cell = ...

        cell.userInteractionEnabled = NO;

    }
Run Code Online (Sandbox Code Playgroud)

享受。:)