2013-10-22 131 views
6

我有一個TableView控制器,我想用數組中的對象填充。我正在使用StoryBoard。另外,我不確定是否需要將標籤作爲佔位符類型放置在故事板的單元原型中?如何用對象數組填充TableView?

My aList Mutable Array包含類型爲Homework的對象。 在表的每一行,我要顯示(這些變量已經在我的作業模式設置):

-ClassName

-AssignmentTitle

-DueDate

這裏是我的目前有

TableViewController.h

@interface AssignmentTableController : UITableViewController 
< 
AssignmentDelegate 
> 

@property(strong,nonatomic) Assignment *vc; 
@property (strong, nonatomic) NSMutableArray *alist;//array was filled with delegation 


@end 

TableViewController.m

#pragma mark - Table view data source 

- (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 [self.alist count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier  forIndexPath:indexPath]; 
Homework *ai = [self.alist objectAtIndex:indexPath.row]; 

//*******I am not sure what to do from Here****** 
//*******I need to start displaying 1 object per row,displaying what was stated above*** 

    // Configure the cell... 

return cell; 
} 
+1

[從陣列填充一個UITableView]的可能重複(http://stackoverflow.com/questions/4367393/populating-a-uitableview-from-arrays) –

回答

7

我有一個類似的實現。

執行: 我創建了以下方法。 (我已經編輯它們以滿足你的代碼提供。)

-(Homework*) homeworkAtIndex: (NSInteger) index 
{ 
    return [alist objectAtIndex:index] ; 
} 

-(NSInteger) numberOfObjectsInList 
{ 
    return [alist count]; 
} 

而在的UITableViewController委託方法:

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section 

我用這個方法調用

return [self numberOfObjectsInList]; 

在委託方法:

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SubtitleIdentifier] autorelease]; 
    Homework *ai = [self homeworkAtIndex: indexPath.row]; 

    /* your other cell configurations 
    cell.textLabel.text = ai.className; // eg. display name of text 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ | %@",ai.assignmentTitle,ai.dueDate]; // will show the cell with a detail text of "assignment title | due date" eg. "Lab 1 | 23 Oct 2013" appearing under the className called "Physics" 
    */ 
} 

使用homeworkAtIndex方法將允許您將數組中的不同對象填充到表中的不同單元格中。

這種方式讓我通過創建自定義單元格和格式化單元格大小來適應表格。如果將要顯示的數據不是很長,並且實際上並不需要使用自定義單元格,也許這可以適用於您。(很像例子,我提供)

如果你想知道如何檢查選擇的不同的細胞(如你不妨此後他們推到一個不同的viewController),你可以在委託方法做到這一點:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    UIViewController* nextViewController = nil; 
    NSString* cellDisplayName = [delegate homeworkAtIndex: indexPath.row].name; // can be the Homework object if you want 
    if([cellDisplayName isEqualToString:[self homeworkAtIndex:0].className]) 
     nextViewController = [[firstViewController alloc] init]; 
    else if([cellDisplayName isEqualToString:[self homeworkAtIndex:1].className]) 
     nextViewController = [[secondViewController alloc] init]; 
    // goes on for the check depending on the number of items in the array 
} 

我在這裏使用NSString來檢查className,因爲我不確定Homework對象是什麼,但是您肯定可以更改邏輯以適合Homework對象,因爲我的猜測是您可能有不同的對象相同的className變量。

希望這有助於! :)