views:

310

answers:

1

Hello all,

I am developing an iPhone application .

In the application I want to show the uitableview data sorted on the date field :

Suppose I have a Person object with fields name,birthdate,phone number etc.

Now I have array of Person and I am sorting that array on date field.

Now I am not understanding that how to handle these two methods ;

(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath 



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

i.e

How to count date wise objects and determine ?

+1  A: 

Assuming you have one section and a sorted NSArray or NSMutableArray called _personArray:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)_tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)_section {
    return [_personArray count];
}

- (UITableViewCell *) tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)_indexPath {
    Person *_person = [_personArray objectAtIndex:_indexPath.row];
    NSString *_cellIdentifier = [NSString stringWithFormat: @"%d:%d", _indexPath.section, _indexPath.row];
    UITableViewCell *_cell = [_tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
    if (_cell == nil) {
        _cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:_cellIdentifier] autorelease];
    }

    _cell.textLabel.text = _person.name;
    _cell.detailTextLabel.text = _person.birthdate;

    return _cell;
}

If you require multiple sections, split your array into multiple NSMutableArray instances, each containing Person instances associated with a specific section.

Then modify -numberOfSectionsInTableView: to return the number of sections (a count of the number of section arrays), as well as the other two methods to: 1) get element counts within a section array and; 2) to recall the right Person for a given indexPath.section and indexPath.row.

Alex Reynolds
I require multiple section . suppose persons in section(i.e birthdte 16-11-2009) . Can you explain this with little code ?
hib
Make an `NSMutableArray` called `sections`. Each element of `sections` is another `NSMutableArray` containing `Person` instances. You'll just need to program splitting up your set of `Person`s into these sections. Once you have that, you can call `[sections objectAtIndex:indexPath.section]` to get an `NSMutableArray` of `Person`s for a single index. Once you have *that*, you basically have the same code as above, except that you are retrieving a `Person` instance with both `indexPath.section` and `indexPath.row`, instead of just `indexPath.row`.
Alex Reynolds