开发者

Iphone NSArray and Sections

I am currently developing an app to show fixtures in a football competition using data held in NSArrays. I would like to now put the data into section i.e. for example Round 1 showi开发者_运维技巧ng 3 matches, Round 2 Showing 6 matches etc.

Can someone please assist me with how I can show the data in a UITable in sections?

Regards John


I usually use NSArrays in a NSArray for this.

This is how I would create the datasource array:

NSArray *section0 = [NSArray arrayWithObjects:@"Section 0 Row 0", @"Section 0 Row 1", nil];
NSArray *section1 = [NSArray arrayWithObjects:@"Section 1 Row 0", @"Section 1 Row 1", @"Section 1 Row 2", nil];
self.tableViewData = [NSArray arrayWithObjects:section0, section1, nil];

and some UITableView methods so you get the idea:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.tableViewData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.tableViewData objectAtIndex:section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyObject *object = [[self.tableViewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    // Do something
} 

And for the titles (and indexes) of the sections I create separate NSArrays.


better to create Array of Array . suppose you have 3 sections and every section have 4 rows . so create one array with three array object .and each inner array will contains the rows value


You need to create a class that conforms to the UITableViewDataSource protocol. This class is typically a subclass of UIViewController. You should then set the dataSource property of a UITableView to point to an instance of this class. Your data source class should implement the method sin the protocol so as it returns values based in the data in your model (hence this is a controller class). If it helps, you can also use the UIViewController subclass UITableViewController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜