2013-10-22 11 views
6

私はTableView Controllerを持っていて、配列からObjectを取り込んでいます。私はStoryBoardを使用しています。また、プレースホルダーの一種として、ストーリーボードのセルプロトタイプにラベルを配置する必要があるかどうかはわかりません。TableViewにオブジェクトの配列を設定するには?

My aList変更可能な配列には、宿題タイプのオブジェクトが含まれています。テーブルの各行には 、私は(これらはすでに私の宿題モデルに設定された変数です)表示したい:

-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変数。

希望すると便利です。 :)

0

私はあなたを参照してください[1] ちょうどセルのコンテンツビューにラベルを配置し、ビューとしてセルを使用

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

カスタムセルを使用しますあなたが表示したいデータを持つオブジェクトをすでに持っているので、表示したいものを持つセルを使用してください。

3

配列を使用してtableviewsを読み込むチュートリアルがたくさんありますs。これらのチュートリアルを見てください。

http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1

あなたは...デフォルトのテーブルセルは通常1つのイメージ図と2つのラベルを持つ3枚のラベルに対応するためにカスタムセルを実装する必要があります。..

関連する問題