簡體   English   中英

UITableView從NSMutableArray / NSDictionary循環出數據

[英]UITableView Looping Out Data From NSMutableArray / NSDictionary

我目前正在構建一個iPhone應用程序,該應用程序將顯示來自NSMutableArray的名為“故事”的數據。 數組結構如下所示(通過NSLog):

    2009-07-20 12:38:30.541 testapp[4797:20b] (
    {
    link = "http://www.testing.com";
    message = "testing";
    username = "test";
},
    {
    link = "http://www.testing2.com";
    message = "testing2";
    username = "test2";
} )

我的cellForRowAtIndexPath當前看起來像這樣:

- (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];


}

    for (NSDictionary *story in stories) {
        [cell setTextColor:[UIColor whiteColor]];
        [cell setFont: [UIFont systemFontOfSize:11]];
        cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];
    }
            return cell;
}

當前,我的UITableView顯示SAME項的多個條目(恰好是數組中的最后一組)。 我如何獲取它以成功遍歷數組並依次在單元格中顯示下一項的消息。

提前致謝 :)

石磊

您誤會了cellForRowAtIndexPath:方法的工作方式。

使用它的方式是,創建一個單元格,然后反復重置其文本,textColor和font屬性,然后返回一個單元格。

理解您的問題的關鍵是理解cellForRowAtIndexPath:被多次調用,對於每個將在屏幕上顯示的單元格一次。 因此,請執行以下操作,而不要使用for()循環:

NSDictionary * story = [stories objectAtIndex:[indexPath row]];
[cell setTextColor:[UIColor whiteColor]];
[cell setFont: [UIFont systemFontOfSize:11]];
cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];

傳入的indexPath參數是tableView如何指示其要您選擇的單元格。 我們用它來從您的數組中獲取相應的故事字典。

編輯:

我還要指出,此代碼與iPhone OS 3.0不兼容。 3.0 SDK引入了對UITableViewCell工作方式的更改,包括其視圖層次結構。 您可能想要訪問單元格的textLabel,然后設置that的屬性,如下所示:

NSDictionary * story = [stories objectAtIndex:[indexPath row]];
[[cell textLabel] setTextColor:[UIColor whiteColor]];
[[cell textLabel] setFont: [UIFont systemFontOfSize:11]];
[[cell textLabel] setText:[NSString stringWithFormat:[story objectForKey:@"message"]]];

這里不需要循環-您要做的是使用表單元格的索引並獲取數組的相應元素(尚不清楚,您正在使用數組還是字典?)。 因此,例如,數組的第四個元素將放置在第四個表單元格中。

解決方法是:

 NSDictionary *story = [stories objectAtIndex: indexPath.row];
cell.text=[NSString stringwithFormat:[story objectForKey@"message];

你這里的問題是

for (NSDictionary *story in stories) {       

[cell setTextColor:[UIColor whiteColor]];
[單元格setFont:[UIFont systemFontOfSize:11]];
cell.text = [NSString stringWithFormat:[story objectForKey:@“ message”]];
}

您正在設置單元格以始終顯示最后一個故事,您想要做什么,例如

NSDictionary *story = [stories objectAtIndex: indexPath.row];
cell.text=[NSString stringwithFormat:[story objectForKey@"message];

代替

for (NSDictionary *story in stories) {
    cell.text = [NSString stringWithFormat:[story objectForKey:@"message"]];
}

你要

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.text = [NSString stringWithFormat:[[stories objectAtIndex: storyIndex] objectForKey:@"message"]];

您的cellForRowAtIndexPath方法將為每個單元cellForRowAtIndexPath一次,因此您只想設置該特定單元格的文本。

請注意,對於沒有節的簡單表,“索引路徑”最終只是其中包含一個整數的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM