tags:

views:

53

answers:

2

UITableView not scrolling smoothly...(iPhone SDK) ..!!

I have implemented UITableView DataSource and Delegate methods in an individual separate classes.(one for delegate and one for datasource) in main program i write only:

//assume that all objects are allocated 
ObjTableView.dataSource=ObjDataSource;
ObjTableView.delegate = ObjDelegate;
[self.view addSubView: ObjTableView];

when i run this code , UITable view appears but when i try to scroll it, it doesn't scroll smoothly. I have also checked that UITableViewCell doesn't redraw once the cell is initialized.

can any one tell me why this happens ? How can i solve this problem ??

From comments:

ListDataSource *ObjListDataSource = [[ListDataSource alloc]initWithArray:[[sender object] valueForKey:@"List"]]; 
ListDelegate *ObjListDelegate = [[ListDelegate alloc]initWithArray:[[sender object] valueForKey:@"List"]];
tblList = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];  
tblList.dataSource = ObjListDataSource; tblList.delegate = ObjListDelegate; 
[self.view addSubview:tblList]; [tblShopList release];

More from comments:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,100) reuseIdentifier:CellIdentifier] autorelease]; 
        //custom cell code 
    } 
    return cell; 
}

More Information:

I have used NSNotification which notifies to current class when parsing is complete, after receiving notification , current class method calls DataSource, Delegate methods (which is defined in a separate class file).

So UItableViewCell customization (which is in ListDataSource) and table view(in current class) both are in different classes.

+4  A: 

A problem is

NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 

The id needs to be the same for all cells of the same class, otherwise you never reuse them. As you can see in most examples, it is indeed a constant in most (all?) cases.

Little explaination on the reuseIdentifier: every time a cell gets out of screen, you can reuse it instead of creating a new one. To reuse it, you need a cell in queue with the same identifier as the one you pass to dequeueReusableCellWithIdentifier. The way you did, the cells are never reused, because each id is unique (they may or may not be reused in case a row reappears on screen, depending on queue size, which is not configurable AFAIK). This is why personalization of the cell should happen OUTSIDE the "cell == nil" block. Long story short, you are using the reuseIdentifier not as intendend.

Michele Balistreri
I don't think so, mostly we are having unique cellidentifier for each individual cell.
Matrix
Little explaination on the reuseIdentifier: every time a cell gets out of screen, you can reuse it instead of creating a new one. To reuse it, you need a cell in queue with the same identifier as the one you pass to dequeueReusableCellWithIdentifier. The way you did, the cells are never reused, because each id is unique (they may or may not be reused in case a cell reappears on screen). This is why personalization of the cell should happen OUTSIDE the "cell == nil" block. Long story short, you are using the reuseIdentifier not as intendend.
Michele Balistreri
In case of NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row];here cells are reused when they are reappear on the screen. I have tested it on device. In this case cell identifier is unique for an individual cell. So for cell1-> Identifier-1,.....,Cell10-> Identifier-10.
Matrix
Table cell not even scrolling smoothly when no cell gets out of screen.
Matrix
so in that case i think cell re usability is not an issue.
Matrix
How can you scroll if no cell gets out of screen? Anyway the behaviour you described is true, but is not what the reuse queue is for. Anyway, maybe you load some huge images in the cell? From the code you posted there aren't much elements to determine the cause of the performance hit
Michele Balistreri
when i scroll cell from top to bottom or vice versa. it became freeze at final position. then when i touch on screen it agin goes to initial position.
Matrix
You need to have the same CellIdentifier for all cells that have the same layout / class. Michele Balistreri is right. Just use the CellIdentifier as intended.
Sander Backus
You can use the 'tag' property of UITableView/UIVIew to identify your cells later.
Sander Backus
A: 

I think Michele is correct, but I would also add that it looks like you are doing your cell customization where the cell gets created. What you should be doing is something more like this:

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

    NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = (UITableViewCell)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,100) reuseIdentifier:CellIdentifier] autorelease]; 
        //custom REUSABLE cell code here, e.g. text color, etc. 
    }

    NSString *cellText = [dataArray objectAtIndex:indexPath.row]; //assuming you have a simple array for your data

    cell.textLabel.text = cellText;

    return cell; 
}

I would also add that I'm not sure why you are able to run the app with the code you have here, since UITableViewCell cell = ... is an invalid initializer. It should be UITableViewCell *cell = ....

It would be helpful to see how you are customizing your cell, since without that it's hard to see what's happening.

JoBu1324
Since you don't include any of your cell initialization code, I thought it might also be worth mentioning that adding subviews is something that should be done once - i.e. that should be done in the if (cell == nil) {} block where it is cached for all cell use.
JoBu1324