2011-08-06 36 views
0

我有以下格式的數組:如何將數組結構化爲UITableView?

[NSArray arrayWithObjects: 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
[NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil], 
nil]; 

我想結構的數據加載到我的tableView。

tableview的每一行應該對應數組的每一行,那麼每個單元格的標題應該對應於名稱的子數組objectAtIndex 2。

+0

在我看來,這個代碼已經'結構化'爲'UITableView'。你在尋找代碼來實現'UITableViewCell'的創建嗎? – GarlicFries

+0

它看起來像你這樣做是錯誤的...你需要更具體的關於你希望如何顯示桌面 –

+0

是的,我想是的,看着設置單元格。像cell.textlabel.text = [數組對象在什麼?]]因爲它是一個數組內的數組。 – Jon

回答

2

假設你的數組被命名爲myData

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [myData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    NSArray *obj = [myData objectAtIndex:indexPath.row]; 
    cell.textLabel.text = (NSString*)[obj objectAtIndex:1]; //note: 0=>number; 1=>name,.. 

    return cell; 
} 

贊成的可重用性,我建議用NSDictionaries替換子陣列,這樣你可以得到如該名稱通過致電[dict objectForKey:@"name"]