Vishnu
Vishnu

Reputation: 208

Image in tableviewcell slow down scrolling

My tableview consists of images loading from server, this slow down my tableview scrolling. Below is my code . any idea, please help me.

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


    CustomTblViewCellFacetoface *cell = (CustomTblViewCellFacetoface *) [tableView dequeueReusableCellWithIdentifier:@"cellA"];
    if  (cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTblViewCellFacetofaceNib" owner:Nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if  ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell =  (CustomTblViewCellFacetoface *) currentObject;

                break;
            }
        }
    }
    // configure cell

    IStructFacetofaceRequests *objappointmentdetails  =   [M_ArrFacetofaceRequests objectAtIndex:indexPath.row];   
    cell.selectionStyle                         =   UITableViewCellSelectionStyleNone;
    cell.backgroundColor                        =   [UIColor clearColor];
    cell.m_CtrllblName.text                     =   objappointmentdetails.m_strUsername;
    cell.m_CtrllblVenue.text                     =   [NSString stringWithFormat:@"Venue : %@",objappointmentdetails.m_strVenue];
    cell.m_CtrllblDate.text                     =   [NSString stringWithFormat:@"%@ - %@",objappointmentdetails.m_strStartDate,objappointmentdetails.m_strEndDate];
    [cell.m_CtrllblName setTextColor:[UIColor colorWithRed:(113/255.f) green:(113/255.f) blue:(113/255.f) alpha:1.0f]];
    [cell.m_CtrllblVenue setTextColor:[UIColor colorWithRed:(113/255.f) green:(113/255.f) blue:(113/255.f) alpha:1.0f]];
    [cell.m_CtrllblDate setTextColor:[UIColor colorWithRed:(113/255.f) green:(113/255.f) blue:(113/255.f) alpha:1.0f]];
    cell.m_CtrllblName.font=[UIFont fontWithName:@"Arial-BoldMT" size:16];
    NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: objappointmentdetails.m_strImageurl]];

    cell.m_CtrlImgViewUser.image=[UIImage imageWithData: imageData];
    [cell.m_CtrlBtnView addTarget:self action:@selector(MoveToNextView:) forControlEvents:UIControlEventTouchUpInside];
    cell.m_CtrlBtnView.tag=indexPath.row;

    return cell;

}

this is the code i used in cellfor row at index path.

Upvotes: 0

Views: 218

Answers (2)

Omarj
Omarj

Reputation: 1159

You can use UITableView lazy loading .

here an sample from apple

and u can see this project at github

Yes, UITableView is designed around the assumption of lazy loading. When you make a UITableView, you do not load any of the entries at all. Instead you wait for the system framework to call your cellForRowAtIndexPath method. That method gets called once for every cell that needs to be loaded, and that is just for the cells that are visible at the time. As the user scrolls the table view, new cells come into view, and your cellforRowAtIndexPath method gets called again for each new cell coming into view.

Now that is the general principle. But if your cells are being populated by data from a server, then there are some additional considerations. You could structure your cellForRowAtIndexPath to be as lazy as possible and call the server for each and every cell that comes into view. But the network delays would make the user experience really awful. So it is to your advantage to buffer up a number of cells worth of data ahead of time in a data structure other than the cells in a UITableView. That way, when cellForRowAtIndexPath gets called, you will be able to quickly supply the contents of the cell by constructing it from the buffered data. Exactly how much data to buffer depends on how large each data element is, and what else your application is doing in the way of memory allocation. Offhand, I see nothing wrong with buffering 500 to 1000 cells worth of data in your app. But don't mix up your buffering of the data with the UITableView's queuing and reusing of cells. There is no reason to maintain a large number of cells ready to go - just the data that goes into those cells.

Upvotes: 1

Murali
Murali

Reputation: 1889

You can solve that by using Asynchronous loading.. please go through this tutorial..

http://www.markj.net/iphone-asynchronous-table-image/

Upvotes: 1

Related Questions