2011-08-24 27 views
8

Tôi gặp sự cố khi thêm các bản xem phụ vào UITableViewCell. Nó hoạt động khi kích thước bảng dưới kích thước iPhone.thêm các bản xem phụ vào UITableViewCell

Nhưng khi kích thước lớn, nó làm cho một số hiệu ứng khủng khiếp như thế này khi tôi đang di chuyển:

enter image description here

Nó nghĩa vụ phải được như thế này:

enter image description here Sau đó, tôi nghĩ rằng nó đến từ việc tái sử dụng tế bào. Dưới đây là một mẫu mã của tôi:

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

    static NSString *kCellIdentifier = @"UITableViewCellStyleSubtitle"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; 
    if (cell == nil) { 
     //construct the cell 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:kCellIdentifier] autorelease]; 


     //clear the previuous content 
     NSLog(@"Il y a %d subviews", [[[cell contentView] subviews] count]); 
     //[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)]; 
     NSLog(@"Il y a %d subviews", [[[cell contentView] subviews] count]); 
     [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; 
     [cell setSelectionStyle:UITableViewCellEditingStyleNone]; 
    }  

    switch (indexPath.row) { 
     case 0: 
      [cell addSubview:titleEvent]; 
      break; 
     case 1: 
      //load the owner logo 
      [cell addSubview:logoAsso]; 
      break; 
     case 2: 
      //StartDate 
      [cell addSubview:clockImage]; 
      break; 
     case 3: 
      //EndDate 
      [cell addSubview:clockEndImage]; 
      break; 
     case 4: 
      //Address 
      [cell addSubview:adress]; 
      break; 
     case 5: 
      //map 
      [cell addSubview:map]; 
      break; 
     case 6: 
      //header 
      [Graphism configureSeparationCell:cell]; 
      break; 
     case 7: 
      //descritpion 
      [cell addSubview:descriptionEvent]; 
      break; 
     default: 
      break; 
    } 
    return cell; 
} 

Các subviews là attributs của lớp, và cấu hình trong phương pháp viewDidLoad. Nếu bạn có thể cho tôi biết những gì tôi đang làm sai, đó sẽ là một cứu trợ.

Trả lời

4
switch (indexPath.row) { 
    case 0: 


     if (![cell.contentView viewWithTag:11]) { 

      titleEvent.tag = 11; 

      [cell.contentView addSubview:titleEvent]; 
     } 


     break; 
    case 1: 
     if (![cell.contentView viewWithTag:11]) { 

      logoAsso.tag = 11; 

      [cell.contentView addSubview:logoAsso]; 
     } 

như thế này làm cho tất cả các trường hợp chuyển đổi

-3

[cell.contentView addSubview: clockImage];

5

Hãy thử mã này

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"] autorelease]; 

    } else { 
     UIView *subView = (UIView *)[cell.contentView viewWithTag:1]; 
     if ([subView superview]) { 
      [subView removeFromSuperview]; 
     } 
    } 

    UIView *subView = [[UIView alloc] init]; 
    subView.tag = 1; 
    [cell.contentView addSubview:subView]; 
    [subView release]; 

    return cell; 
} 
Các vấn đề liên quan