2010-02-21 48 views
1

我正在cellForRowAtIndexPath,我試圖嵌入到單元格的文本。現在,嵌入部分一切都很好,但當我嘗試加載除第一行之外的所有文本視圖時,我遇到了最奇怪的問題。iphone sdk - 與TableView單元格的奇怪錯誤

注意:我希望TextView除了表格中的第一行外。

問題是,當我執行if語句檢查indexpath.row,然後向下滾動文本視圖時,在某些單元格中不可見。它與我有着相當大的高度> 200的細胞有關,它們最初在屏幕外。當我向下滾動時,就像下面最下面一個可見單元格下面的單元格現在正在響應== 0的索引路徑行。

我完全困惑,有沒有人有任何想法?

我的代碼

// Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    CGRect frame; frame.origin.x = 5; frame.origin.y = 10; frame.size.width = 20; frame.size.height = 25; 

      // ..some unrelated setup in here 

    //textbox setup 
    textView = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)]; 
    textView.layer.cornerRadius = 10; 
    textView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.2]; 
    textView.font = [UIFont fontWithName:@"Helvetica" size:17]; 
    textView.text = @"test-content"; 
    textView.layer.borderWidth = 1; 
    textView.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor]; 
    textView.tag = 4; 

    if (indexPath.row != 0) { 
    [cell.contentView addSubview:textView]; 
    [textView release]; 
    } 
    } 
    // a little more unrelated stuff 


return cell; 

}

有人能看出問題?

感謝

+1

我很難想象您的描述。你能附上截圖嗎? – Giao 2010-02-21 23:42:03

回答

1

幹嘛還要設置TextView的時候indexPath.row不是零(如果indexPath.row是零,你不鬆開)?

嘗試將創建和設置移動到「if(indePath.row!= 0)」塊(位於addSubView行上方)內部。

+0

這是我以前試過的,有些東西還是有點奇怪。當我將if()更改爲switch/case函數時發生了一個修復。 – oberbaum 2010-02-22 10:55:28

0

您的問題可能是當您創建單元格時,您給它們所有相同的單元格標識符。當第一小區排隊,但沒有一個文本視圖,但是你的代碼,這部分被稱爲:

if (cell == nil) 

小區被退回,而不是nil,但在第一的情況下,單元格,它沒有文本視圖。嘗試更改第一個單元格的單元格標識符以查看是否有更好的結果;這將阻止它被重新使用。

+0

這不是我在下面的代碼中建議的嗎? – FelixLam 2010-02-22 00:35:46

+0

您的代碼爲每個單元格創建一個文本視圖,並在單元格位於第0行時隱藏它。這會導致創建不必要的文本視圖。你提出這個建議,然後做相反的事。 – 2010-02-22 01:39:17

+0

我已更新代碼。 – FelixLam 2010-02-22 10:15:43