开发者

Adding new rows to UITableViewCell

In my application,I will be displaying only one row on the UITableView initially. I want to increase the rows as user loads the previous row with data(an uiimage, here). As now i'm returning value 1, in numberOfRowsInSection: method, since I don't know how to implement it in the required way. Please help.

My cellForRowAtIndexPath: method is

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

     CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier ] autorelease];

         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];      
         for (id currentObject in nib){
             if ([currentObject isKindOfClass:[CustomCell class]]){
                 cell = (CustomCell *)currentObject;
                 cell.viewController = self;
                 break;
             }
         }          
     }
     if (j<15){
                 cell.imageView.image = nil;
         if (count!=0)
         {
             @try{
                NSInteger row = [indexPath r开发者_运维百科ow];
                 cell.imageView.image = [array objectAtIndex:row];
                  }
             @catch (NSException* ex) {
                 NSLog(@"doSomethingFancy failed: %@",ex);
             }
         }
     }
     cell.showsReorderControl = YES;
      return cell;
     [array release];
 }

that if condition and count is nothing but just for checking the correct functioning of the mutable array, 'array'.


You should read this documentation: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html

Have a look to the related samples as well.

Note: in your code [array release] won't be called since you've got a return statement just before it.


When you reach the cellForRowAtIndexPath at a certain indexPath, it means that row is being displayed. If you reach the last row you can insert a new row into the table. To do that, you just need to increase the count that will get returned by the tableView:numberOfRowsInSection function.

I'm guessing your numberOfRowsInSection function will look something like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section==0 ? [array count] : 0;
}

So basically, inside cellForRowAtIndexPath, add the following code:

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

     if([indexPath row]==[array count]-1) {
         [array addObject:(NEXT_IMAGE)];
     }
     return cell;
 }

You also need to refresh the table. One way is to just call reloadData on your UITableView, but the preferred way is to call beginUpdates/insertRowsAtIndexPaths/endUpdates on the table so that you can show a nice animation of the row being inserted.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜