2011-09-11 32 views
1

我試圖用兩種不同類型的UITableViewCells打印UITableView。包含任何操作(第一2細胞),只是一個黑色背景 來自UITableView的怪異行爲

  • 表視圖細胞包含一個標籤,圖像等從1開始

    1. 空表視圖細胞(第三和隨後的細胞)

    我的表格視圖的高度在任何時候都能夠適應至少3個表格視圖單元格。

    當我第一次加載表視圖時,表視圖看起來非常好 - 前兩行是黑色的,後面跟着第三行,標籤上寫着「1」。

    但是,滾動到底部然後滾動回來。我的前兩個空單元格(應該是空的)填充了只能在第三個單元格和後續單元格中找到的內容。

    我懷疑這種行爲來自表視圖重複使用單元格,但我仍然無法弄清楚。

    代碼片段:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"Cell"; 
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        if (cell == nil) { 
         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
        }  
    
        switch ([indexPath row]) { 
         case 0: 
          cell.contentView.backgroundColor = [UIColor blackColor]; 
          [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
          break; 
         case 1: 
          cell.contentView.backgroundColor = [UIColor blackColor]; 
          [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
          break; 
    
         default: 
          cell.contentView.backgroundColor = [UIColor blackColor]; 
          [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    
          UILabel *rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)]; 
          rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1]; 
          rowLabel.backgroundColor = [UIColor blackColor]; 
          rowLabel.textColor = [UIColor whiteColor]; 
          [rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]]; 
          [cell.contentView rowLabel];     [rowLabel release]; 
    
          UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)]; 
          [aButton setImage:anImage forState:UIControlStateNormal]; 
          [cell.contentView addSubview:aButton]; 
          [aButton release]; 
    
          break; 
        } 
    
        return cell; 
    } 
    
  • +0

    我可能找到了我的解決方案。我採取了使用唯一重用標識符的建議。我爲每個細胞都做了這個。而且,我將開關盒移到了if中。當stackoverflow允許我發佈我的答案! – tommi

    +0

    這不是正確的解決方案,您應該儘可能重複使用單元格,否則您的滾動可能會非常緩慢。 – jrturton

    +0

    @ jrturton,謝謝你讓我知道!當你指重用時,你的意思是重用單元內的每一件事物(包括按鈕,圖像)? – tommi

    回答

    1

    您的出隊單元格仍將包含您添加的所有按鈕和標籤,您所做的只是在單元格出列時設置背景顏色。

    您有幾種選擇:

    • 使用tableHeaderView您的黑色區域
    • 使用帶有部分
    • 使用細胞行0和1
    • 不同的重用標識符的分組表視圖

    對於後一選項,您cellForRowAtIndexPath應該是:

    static NSString *CellIdentifier = @"Cell"; 
    static NSString *blankCellIndentifier = @"BlankCell"; 
    
    if (indexPath.row == 0 || indexPath.row == 1) 
    { 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:blankCellIdentifier]; 
        if (cell == nil) 
        { 
         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:blankCellIdentifier] autorelease]; 
         cell.contentView.backgroundColor = [UIColor blackColor]; 
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
        } 
    return cell; 
    } 
    else 
    { 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        UILabel *rowLabel; 
        UIButton *aButton; 
    
        if (cell == nil) 
        { 
         // Here, you create all of the new objects 
         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
         cell.contentView.backgroundColor = [UIColor blackColor]; 
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
         rowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 30, 200, 30)]; 
         rowLabel.backgroundColor = [UIColor blackColor]; 
         rowLabel.textColor = [UIColor whiteColor]; 
         [rowLabel setFont:[UIFont fontWithName:@"GillSans-Bold" size:18]]; 
         [cell.contentView addSubview:rowLabel]; 
         rowLabel.tag = 1; 
         [rowLabel release]; 
    
         aButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 40, 40)]; 
         [aButton setImage:anImage forState:UIControlStateNormal]; 
         [cell.contentView addSubview:aButton]; 
         aButton.tag = 2; 
         [aButton release]; 
    
        } 
        // Here, you just configure the objects as appropriate for the row 
        rowLabel = (UILabel*)[cell.contentView viewWithTag:1]; 
        rowLabel.text = [NSString stringWithFormat:@"Row %d", [indexPath row]-1]; 
    
        return cell; 
    } 
    
    +0

    我試圖在第0行和第1行使用不同的重用標識符。例如:'CellBlank'和'Cell'? CellBlank for 1st two rows'Cell for the 3rd and following rows? – tommi

    +0

    這會工作,你需要幫助嗎? – jrturton

    +0

    非常感謝。我剛剛實現了這些代碼,至今我的應用程序運行良好! – tommi

    0

    對於每種不同類型的細胞,需要一個不同的重用標識符。所以,你的情況你需要三個reuseIdentifier的。

    +0

    Hi @Benjamin Mayo,爲什麼3而不是2?既然我有兩種不同類型的細胞? – tommi

    +0

    您的代碼中有三個單元格。當行在索引0,索引1和任何其他行時。這是三種類型的細胞。 –

    +0

    我已經得到了上面的答案。感謝您的幫助。 – tommi