2012-11-23 55 views
0

我的tableview包含從服務器加載的圖像,這減慢了我的tableview滾動。以下是我的代碼。任何想法,請幫助我。tableviewcell中的圖像減慢滾動

- (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; 

} 

這是我在cellfor行索引路徑中使用的代碼。

+0

你的圖像的分辨率? – ignotusverum

+0

仍然有問題? – Omarj

回答

1

您可以使用UITableView延遲加載。

這裏的sample從蘋果

和u可以看到這個項目在github

是,UITableView是圍繞延遲加載的假設。當你製作UITableView時,根本不加載任何條目。相反,您需要等待系統框架調用您的cellForRowAtIndexPath方法。對於每個需要加載的單元格,該方法會被調用一次,並且這隻適用於當時可見的單元格。當用戶滾動表格視圖時,新的單元格進入視圖,並且您的cellforRowAtIndexPath方法會再次被調用,以便每個新的單元格進入視圖。

現在,這是一般原則。但是,如果您的單元格正在被來自服務器的數據填充,那麼還有一些額外的考慮因素。你可以將你的cellForRowAtIndexPath構造得儘可能的懶惰,併爲每個進入視圖的單元調用服務器。但是網絡延遲會讓用戶體驗變得非常糟糕。因此,在UITableView中的單元之外的數據結構中,提前緩衝大量數據的單元格對您有利。這樣,當cellForRowAtIndexPath被調用時,您將能夠通過從緩衝數據構建單元來快速提供單元格的內容。確切地說有多少數據要緩衝取決於每個數據元素的大小以及您的應用程序在內存分配方面做了些什麼。不速之客,我發現在你的應用中緩衝500到1000個單元的數據是沒有問題的。但是不要將你的數據緩衝與排隊和重用單元混淆。沒有理由保持大量的細胞準備好 - 只是進入這些細胞的數據。