2013-11-21 111 views
0

當我使用dequeReusableCell,當我向下滾動或向上滾動最後單元格(即從可見屏幕出去)的內容時會重疊,但每次創建新單元格時都會起作用。 我想使用dequereusble功能。使用dequereusable單元格重疊滾動後的uitableview單元格的內容

下面的代碼工作,但當我取消註釋dequereusable問題的部分再次出現。

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

static NSString *[email protected]"Cell"; 

    //Code causing problem 
    /*UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(!cell) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    }*/ 

    //Code working fine 
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 20)]; 
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15]]; 

    UIImageView *headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)]; 
    headerImageView.image=[UIImage imageNamed:@"offer_details.jpg"]; 

    UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)]; 
    [detailLabel setFont:[UIFont systemFontOfSize:14]]; 

    UIButton *callButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 20, 20)]; 
    [callButton setBackgroundColor:[UIColor orangeColor]]; 

    MKMapView *mapView=[[MKMapView alloc] initWithFrame:CGRectMake(10, 30, 320, 200)]; 

    switch (indexPath.row) { 
     case 0: 
      [cell.contentView addSubview:headerImageView]; 
      [cell setBackgroundColor:[UIColor blackColor]]; 
      [email protected]"Get great offer Upto 20% Off"; 
      titleLabel.frame=CGRectMake(10, 220, 230, 20); 
      titleLabel.textColor=[UIColor colorWithRed:251.0/255.0f green:78.0f/255.0f blue:8.0f/255.0f alpha:1]; 
      detailLabel.frame=CGRectMake(10, 240, 300, 20); 
      detailLabel.textColor=[UIColor whiteColor]; 
      [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt."; 
      [cell.contentView addSubview:detailLabel]; 
      break; 
     case 1: 
      [email protected]"Details"; 
      [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "; 
      [cell.contentView addSubview:detailLabel]; 
      break; 
     case 2: 
      [email protected]"Collection Methods"; 
      [email protected]"Call and Reserve"; 
      detailLabel.frame=CGRectMake(50, 30, 300, 20); 
      [cell.contentView addSubview:detailLabel]; 
      [cell.contentView addSubview:callButton]; 
      break; 
     case 3: 
      [email protected]"Location"; 
      [cell.contentView addSubview:mapView]; 
      break; 
     case 4: 
      [email protected]"About us"; 
      [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."; 
      [cell.contentView addSubview:detailLabel]; 
      break; 
     case 5: 
      [email protected]"Legal"; 
      [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."; 
      [cell.contentView addSubview:detailLabel]; 

      break; 
     default: 
      break; 
    } 

    detailLabel.numberOfLines=0; 
    [detailLabel sizeToFit]; 
    [cell.contentView addSubview:titleLabel]; 


    return cell; 
} 

編輯:

我將能夠使用dequereusable細胞,因爲我行的內容是不常見?一行包含圖像,一個包含mapview,一個僅包含標籤。提前Thanx。

EDIT2:

@HRM現在其示出細節拉布勒空白除第一小區

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

{ 靜態的NSString * cellIdentifier = @ 「小區」;

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

//UITableViewCell *cell; 

if(!cell) 
{ 
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 20)]; 
    [titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
    titleLabel.tag=1001; 

    [cell.contentView addSubview:titleLabel]; 
    switch (indexPath.row) { 
     case 0: 
     { 
      UIImageView *headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)]; 
      headerImageView.image=[UIImage imageNamed:@"offer_details.jpg"]; 
      headerImageView.tag=1002; 

      UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)]; 
      [detailLabel setFont:[UIFont systemFontOfSize:12]]; 
      detailLabel.tag=1003; 
      detailLabel.numberOfLines=0; 
      [detailLabel sizeToFit]; 
      [cell.contentView addSubview:headerImageView]; 
      [cell.contentView addSubview:detailLabel]; 
     } 
      break; 

     case 1: 
     { 
      UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)]; 
      [detailLabel setFont:[UIFont systemFontOfSize:12]]; 
      detailLabel.numberOfLines=0; 
      [detailLabel sizeToFit]; 
      [cell.contentView addSubview:detailLabel]; 
      detailLabel.tag=1004; 
     } 
      break; 
     case 2: 
     { 
      UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)]; 
      [detailLabel setFont:[UIFont systemFontOfSize:12]]; 
      UIButton *callButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 15, 15)]; 
      [callButton setBackgroundColor:[UIColor orangeColor]]; 
      detailLabel.numberOfLines=0; 
      [detailLabel sizeToFit]; 
      [cell.contentView addSubview:detailLabel]; 
      detailLabel.tag=1005; 
      [cell.contentView addSubview:callButton]; 
     } 
      break; 
     case 3: 
     { 
      MKMapView *mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0, 30, 320, 140)]; 
      [cell.contentView addSubview:mapView]; 
     } 
      break; 
     case 4: 
     { 
      UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)]; 
      [detailLabel setFont:[UIFont systemFontOfSize:12]]; 
      detailLabel.tag=1006; 
      detailLabel.numberOfLines=0; 
      [detailLabel sizeToFit]; 
      [cell.contentView addSubview:detailLabel]; 
     } 
      break; 
     case 5: 
     { 
      UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)]; 
      [detailLabel setFont:[UIFont systemFontOfSize:12]]; 
      detailLabel.tag=1007; 
      detailLabel.numberOfLines=0; 
      [detailLabel sizeToFit]; 
      [cell.contentView addSubview:detailLabel]; 
     } 
      break; 
     default: 
      break; 
    } 
} 


switch (indexPath.row) { 
    case 0: 
    { 
     [cell setBackgroundColor:[UIColor blackColor]]; 
     UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001]; 
     UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1003]; 
     [email protected]"Get great offer Upto 20% Off"; 
     titleLabel.frame=CGRectMake(10, 210, 230, 20); 
     titleLabel.textColor=[UIColor colorWithRed:251.0/255.0f green:78.0f/255.0f blue:8.0f/255.0f alpha:1]; 
     detailLabel.frame=CGRectMake(10, 230, 300, 20); 
     detailLabel.textColor=[UIColor whiteColor]; 
     [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt."; 
    } 
     break; 
    case 1: 
    { 
     UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001]; 
     UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1004]; 
     [email protected]"Details"; 
     [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "; 
    } 
     break; 
    case 2: 
    { 
     UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001]; 
     UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1005]; 
     [email protected]"Collection Methods"; 
     [email protected]"Call and Reserve"; 
     detailLabel.frame=CGRectMake(30, 30, 300, 20); 

    } 
     break; 
    case 3: 
    { 
     UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001]; 
     [email protected]"Location"; 
    } 
     break; 
    case 4: 
    { 
     UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001]; 
     UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1006]; 
     [email protected]"About us"; 
     [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \n\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."; 
    } 
     break; 
    case 5: 
    { 
     UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001]; 
     UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1007]; 
     [email protected]"Legal"; 
     [email protected]"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \n\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."; 

    } 
     break; 
    default: 
     break; 
} 





return cell; 

}

+2

爲什麼你註釋掉'if(!cell)'的條件? –

+0

它與可重複使用的單元格有關嗎? – ViruMax

+0

你必須確保'dequeue'正確發生,所以你需要檢查。 –

回答

1

當您使用dequeueReusableCellWithIdentifier,該會的tableView重新使用現有的細胞,並在你的代碼,你想一次又一次地創造新的控件,因此它會重疊。這樣做的最好方法就是這樣。請通過這個。希望能幫助到你。

根據您的評論更新。

static NSString *cellIdentifier= @"Cell"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

//If tableView dont have cell available for reuse, then 
//create a new cell and attach the controls to it 
if(cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    if(indexPath.row == 0){ 

     UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 20)]; 
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15]]; 
     titleLabel.tag = 1002; 
     [cell.contentView addSubview:titleLabel]; 
    } 
} 

//Now we have a cell object with controls attached. 
//Now to update the contents on the control, just get the control from cell 
//using its tag and set your data from datasource 
if(indexPath.row == 0){ 
    UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1002]; 
    titleLabel.text = //Text from your datasource for this row 
} 

請注意,您還需要注意heightForRowAtIndexPath中的高度。同樣的邏輯也將應用於那裏。

+0

thanx爲我的幫助,我認爲這是正確的方式,但問題是,我的tableview單元格的內容不是所有單元格的共同點,所以我不能在檢查行號之前添加它們,因此我將無法重用單元格。你怎麼看? – ViruMax

+0

然後,您可以通過檢查行來在if(cell == nil)塊內創建控件。即如果行== 1,然後創建並附加imageView只..在下面的代碼中,只需使用相同的條件來檢索添加的控件。 – HRM

+0

更新了我的答案。請檢查並讓我知道這是否適用於你。 – HRM

0

這裏就是我會建議:

  1. 創建一個自定義UITableViewCell類,並在其initWithStyle:reuseIdentifier:創建所需的所有子視圖。您還需要將您的子視圖分配給外部使用的屬性。
  2. 告訴您的UITableView對象在viewDidLoad中註冊該課程或NIB。
  3. tableView:cellForRowAtIndexPath:中,您可以訪問單元格的屬性並適當更新它們。

這樣,每次調用tableView:cellForRowAtIndexPath:時都不會重新創建單元格或其子視圖。

+0

自定義uitableview單元格的使用將不會有效,因爲我的單元格結構不是所有單元格都通用的。 – ViruMax

0

我建議通過objective-C枚舉從cellForRowAtInddexPath中的UITableViewCell的內容視圖中刪除子視圖。查看以下代碼以供參考。

for(UIView *view in cell.contentView.subviews){ 
    if ([view isKindOfClass:[UIView class]]) { 
     [view removeFromSuperview]; 
    } 
} 
相關問題