2011-10-13 129 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

相關問題