2011-10-13 113 views
0

我有一个可用视图,但我想禁用某些单元格并保持其他单元格已启用。 我用下面的代码来禁用除了在cellForRowAtIndexPath第一所有的细胞:更改uitableview中的特定单元格

if ([indexPath row] > 0) { 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.userInteractionEnabled = NO; 
    cell.textLabel.textColor = [UIColor lightGrayColor]; 
} 

但不是禁用所有细胞,使第一,它禁用所有的细胞,使最后。为什么会发生?我能做些什么来实现它的工作?

顺便说一句这是我所有的cellForRowAtIndexPath代码:

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

    sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]]; 
    contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]]; 

    if ([indexPath row] > 0) { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.userInteractionEnabled = NO; 
     cell.textLabel.textColor = [UIColor lightGrayColor]; 
    } 
    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = contentsForThisRow; 
    return cell; 
} 
+2

将您所有的代码'cellForRowAtIndexPath:' – Nekto

回答

0

与这一个

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

    sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]]; 
    contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]]; 

    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1  reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = contentsForThisRow; 
    if ([indexPath row] > 0) { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.userInteractionEnabled = NO; 
     cell.textLabel.textColor = [UIColor lightGrayColor]; 
    } 
    return cell; 
} 
+0

谢谢!我不敢相信我有这样一个愚蠢的错误... – Amar

0

替换您的cellForRowAtIndexPath:代码你不显示,你声明的细胞,但是你在做什么是在之前设置userinteractionEnabled 获取实际的单元格!

您必须将此自定义代码放在方法的末尾。

此外,尝试在方法中声明单元格变量,它不能用于其他地方。

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

    sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]]; 
    contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]]; 

    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if ([indexPath row] > 0) { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.userInteractionEnabled = NO; 
     cell.textLabel.textColor = [UIColor lightGrayColor]; 
    } 

    cell.textLabel.text = contentsForThisRow; 
    return cell; 
} 
+0

我是第一个=)) – Nekto

+0

看到答案时间,我的是14分钟前,你的15 ^^ – Geoffroy

+0

是的,我在前面,哈哈。 – Nekto

相关问题