开发者

Adding table cell for each record in a uitableview?

i'm trying to implement a system whereby the user is initially presented with a single tablecell, in a uitableview (grouped style), within a uinavigationview.

  ---------------
+ | add record  |
  ---------------

When they click on the cell, they are pushed onto a new screen where they fill in a few textviews (perhaps imbedded in a tableview's cells)

  ---------------
  | (name)      |
  ---------------
  | (phone num) |
  ---------------

Then when they go back, they can see the new record as well as the 'add record' cell.

  ---------------
  | record  1   |
  ---------------
+ | add record  |
  ---------------

(When they go into record 1 again there would be a delete but开发者_运维百科ton)

Is there any sample code or libraries which would achieve this?


IMHO task is quite easy. You don't need any code examples for that. All you need is to understand how UITableView data source works and how UINavigationView works.

Read this 2 guides:

  • Table View Programming Guide for iPhone OS
  • View Controller Programming Guide for iPhone OS : Navigation Controllers

It should be more then enough to implement this feature.

In few words you should push UITableView into UINavigationController stack with "Add Record" cell. On selection event for this cell you should push another UITableView or your custom view. When user press "Done" button - you should save changed data into your model and pull current view from the UINavigationController stack. After that you should tell your first UITableView that model was changed and it will read it again.

Answer to the comment You should design your data structure. E.g. your model will be NSMutableArray of MyRecord objects. So you check in the tableView:cellForRowAtIndexPath:

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

UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier] 
        autorelease];
}
if[recordsArray count] > 0 {
    // you have some records
    if(indexPath.row < [recordsArray count]) {
       MyRecord *record = [recordsArray objectAtIndex:indexPath.row];
       cell.textLabel.text = record.name;
    } else {
       // last cell is Add Record
       cell.textLabel.text = @"Add Record";
    }
} else {
cell.textLabel.text = @"Add Record";
}   
return cell;
}

That is just an idea. it can contains some stupid things (like 2 hardcoded @"Add Record") but idea should be clear

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜