tags:

views:

101

answers:

1

I created sample tableview application and I have an add button above the tableview, when user pressed the add button only we want to add row to table view.

I am write code like this

- (void)viewDidLoad {
    isEditing = NO;
    Mutarray = [[NSMutableArray alloc]init];
    [super viewDidLoad];
}

- (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.
    if (isEditing)
        return [Mutarray count];
    else
        return 0;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    [TableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    NSLog(@"Row = %d", indexPath.row);
// Configure the cell...
    cell.textLabel.text = [Mutarray objectAtIndex:indexPath.row];
    return cell;
}

//When add button pressed

-(IBAction)Add:(id)sender
{   
    isEditing = YES;

    [Mutarray addObject:[NSString stringWithFormat:@"%d",[Mutarray count]]];

    NSArray *insertIndexPaths = [NSArray arrayWithObjects:
                    [NSIndexPath indexPathForRow:[Mutarray count]-1 inSection:0],
                             nil];

    [self.TableView beginUpdates];
    [self.TableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
    [self.TableView endUpdates];
}

This code works fine.But problem is my tableview height is 418 , it shows only 10 row as visible . So when the 11 row was added it added in tableview but not calling this cellForRowAtIndexPath fuction so i cant able to auto scrolling the page ......The first 10 row it calls the cellForRowAtIndexPath fuction.

So what i my doubts is why the cellForRowAtIndexPath fuction only calls visible rows ? Then how can i auto scroll my tableview ?

Plz help me?

Its really urgent ........ Thanks in advance

+2  A: 

So what i my doubts is why the cellForRowAtIndexPath fuction only calls visible rows ?

It is so for optimization reasons. If table view had created cells for all its rows it could downgrade performance dramatically. Instead of that table view creates cells only for rows that are visible (and probably a couple more to ensure smooth scrolling) and then reuses them for rows that become visible - so actually you can show hundreds of row with just, say, 10 cell objects - it is a huge save.

Then how can i auto scroll my tableview ?

You can scroll right after you added a row in your add method:

    ...
    [table endUpdates];

    [table scrollToRowAtIndexPath:[insertIndexPaths objectAtIndex:0]
           atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

P.S. conventionally method and variable names in objective-c start with lowercase, it is better style to follow that guideline.

Vladimir
Thanks Vladimir. It looks great
dragon