2011-02-22 19 views
4

我試圖在xcode中構建一個應用程序,該應用程序在其他人閱讀rss提要並顯示帖子。我是新的Objective-C,有時候會覺得有點困難。Objective C UITableView - 在更改單元格高度後,表單元格顯示錯誤的內容

我對檢索到的故事(帖子)使用NSMutableArray。每個故事都由一個NSMutableDictionary表示,其中包含帖子的標題,主題,日期和鏈接。所有這些都顯示在UIViewController中的UITableView中。 我已經自定義了我自己的單元格,所以我可以在其中顯示多個標籤。

我的問題是,如果我使用tableView:heightForRowAtIndexPath :,前5個單元格(適合屏幕)顯示好,但如果向下滾動,下一個單元格顯示具有相同的內容與前5(即單元格0 -4顯示正常,單元格5包含單元格0的內容,單元格1的單元格6等)!如果我刪除的tableView:heightForRowAtIndexPath:一切都蠻好的(除了沒有我想要的單元尺寸)

以下是關於代碼看起來:

// NavigationContentsViewController.h 
@interface NavigationContentsViewController : 
UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    UITableView *myTableView; 
    IBOutlet UITableView * newsTable; 
    UIActivityIndicatorView * activityIndicator; 
    CGSize cellSize; 
    NSXMLParser * rssParser; 
    NSMutableArray * stories; 
    NSMutableDictionary * item; // it parses through the document, from top to bottom... 

    NSString * currentElement; 
    NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink; 

} 

@property(nonatomic,retain)NSMutableArray *itemsList; 
@property(nonatomic,retain)UITableView *myTableView; 
- (void)parseXMLFileAtURL: (NSString *)URL; 

//NavigationContentsViewController.m 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

// Configure the cell. 
static NSString *MyIdentifier = @"MyIdentifier"; 
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil){ 
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    // Set up the cell 
    int storyIndex = indexPath.row; 
    //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; 
    //Story title 
    //cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; 
    //cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; 
    cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"]; 
    cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"]; 

    return cell; 
} 

return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *selectedCellItem = [NSString stringWithFormat:@"%d", indexPath.row]; 

    TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]]; 
    fvController.selectedCellItem = selectedCellItem; 
    [self.navigationController pushViewController:fvController animated:YES]; 
    [fvController release]; 
    fvController = nil; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 80; 
} 

任何線索? [編輯:改變int storyIndex = indexPath.row;]

+1

你能解釋一下爲什麼使用'int storyIndex = [indexPath indexAtPosition:[indexPath length] - 1];'而不是'int storyIndex = indexPath.row;'' – knuku

+0

如果有涉及的部分,它確實有點複雜......在這種情況下,我建議您爲每個部分創建多個NSMutableArrays(但是,然後再次,您可能必須創建一個更大的數組來存儲它,或者如果它變得太大,你將不得不直接從磁盤加載)。 – FeifanZ

+0

@ NR4TR首先感謝與我的問題打擾! 正如寫道,這是我第一次應付obj-c,所以有很多次我可以使用錯誤的東西,或用奇怪的方式做事。只是讀你的讚揚,我感到很傻... 無論如何,這不會改變任何關於我的問題:) – CrisDeBlonde

回答

11

這是人們用表格單元重用的常見問題。

此行嘗試重用單元格。這意味着,如果小區0移離屏幕將被重新用作細胞5:如果電池不能被重用你正在創建一個新的

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil){ 
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

,並在下一行是你的問題,你只有在細胞不能被重複使用時才設置細胞。發生5次(對於表格可見時可見的單元格)。

但是,表格之後要顯示的所有單元格都將是已經有內容的重用單元格。

// Set up the cell 
    /*...*/ 

但不要擔心。這很容易解決。您必須將您的單元格的創建與其配置分開。就在轉移一些這樣的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil){ 
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 
    // whatever happened before. You have a valid cell at this point. 

    // Set up the cell 
    int storyIndex = indexPath.row; 
    //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; 
    //Story title 
    //cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; 
    //cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; 
    cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"]; 
    cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"]; 
    return cell; 
} 

編輯:也許我應該讀的問題下一次。但我想我仍然100%正確。

如果我刪除的tableView:heightForRowAtIndexPath:一切都蠻好的(除了沒有我想要的單元尺寸)

我認爲這是巧合。你有多少個細胞?我想大約7或8?一切都很好,因爲你的所有單元格同時可見。所以沒有必要重複使用一個單元,他們都擁有他們應該擁有的內容。

+0

哦,上帝!一切都與你所描述的一樣!現在它是有道理的。感謝您提供解決方案,並解釋爲什麼它應該是這樣的! – CrisDeBlonde

1

使用[indexPath indexAtPosition ...]很可能是您的錯誤來源 - 它沒有得到您正確的索引路徑。

但是,如果你正在創建一個CustomCell(希望在IB?),那麼你應該在IB中設置單元的大小,而不是在代碼中。

+0

使用[indexPath indexAtPosition ...]更改零件int int storyIndex = indexPath.row;但沒有發生變化。 但我仍然好奇,爲什麼改變高度與單元格內容有關,如果刪除heightForRowAtIndexPath,一切都很好。 我在xcode中創建了CustomCell,而不是在IB中。有沒有其他方式可以設置單元格高度(maeby in CustomCell.m)? – CrisDeBlonde

+0

在indexPath.row上執行一個NSLog ...它應該完美工作。 – FeifanZ

+0

您是否通過繼承UITableViewCell並使用initWithFrame之類的方法添加標籤來創建CustomCell:? 在這種情況下,請在CustomCell.m中設置單元格的大小,因爲它沒有更改。 – FeifanZ

相關問題