2014-02-07 3 views
0

화면에서 스크롤되어 표시되는 데이터를 해제하지 않는 UITableView가 있습니다.dequeueReusableCellWithIdentifier가 데이터를 해제하지 않습니다.

각 표 셀은 이름, 날짜 및 그림을 표시해야합니다. 셀의 첫 번째 화면은 재사용되지 않아 시작시 올바르게 작동합니다. 그러나 스크롤을 시작하고 셀을 다시 사용하면 표시되는 데이터는 이전에 저장 한 셀에 누적됩니다 (예 : 1971 년 12 월 15 일에 날짜가 1981 년 7 월 4 일에 누적 됨).

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.title = @""; 

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellIdentifier]; 

    self.nameArray = @[@"list of names"]; 
    self.dateArray = @[@"list of dates"]; 
    self.imageArray = @[@"list of images"]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.nameArray count]; 
} 


- (UIView *)messageFeed:(NSIndexPath *)indexPath 
{ 
    UIView *feedView = [[UIView alloc] init]; 
    //...feedView attributes 

    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(..., ..., ..., ...)]; 
    nameLabel.text = self.nameArray[indexPath.row]; 
    //nameLabel attributes 

    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(..., ..., ..., ...)]; 
    dateLabel.text = self.dateArray[indexPath.row]; 
    //dateLabel attributes 

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(..., ..., ..., ...)]; 
    imageView.image = [UIImage imageNamed: self.imageArray[indexPath.row]]; 
    //imageView attributes 

    [feedView addSubview:nameLabel]; 
    [feedView addSubview:dateLabel]; 
    [feedView addSubview:imageView]; 

    return feedView; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 

    [cell addSubview:[self messageFeed:indexPath]]; 

    return cell; 
} 
+2

셀을 처음 추가했을 때마다 셀을 표시/재사용 할 때마다 하위보기가 추가됩니다. 즉, 내용을 바꾸지 않고 (맨 위에 추가하는 것만으로) 이미지가 공개되지 않습니다. 이 주제를 다루는 SO에 대한 많은 질문이 있습니다. Storyboard 또는 XIB에서 IB의 셀을 디자인하는 것이 더 좋지만, cell == nil 인 경우에만 뷰를 추가하거나, 더 나은 방법으로 뷰를 추가하려면 tableView : cellForRowAtIndexPath : 메소드를 다시 고려해야합니다. 검색을 수행하면 필요한 모든 정보를 찾을 수 있습니다. 예 : http://stackoverflow.com/q/16174358/558933 –

답변

1

이것이 최선의 해결책인지 확실하지는 않지만 저에게 잘 맞는지 확인하십시오. 셀 서브 뷰에 UI 필드를 추가해야 할 때 셀이 항상 0이 아닌 것으로 보입니다.이 문제를 해결하려면 먼저 셀 태그가 설정되어 있는지 확인하십시오. 그렇지 않으면 모든 UI 필드를 셀 하위보기에 다시 추가합니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{ 
    UILabel *background; 
    UILabel *nameLabel; 
    UILabel *dateLabel; 
    UIImageView *imageView; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTableViewCellIdentifier]; 
    } 

    background = (UILabel *) [cell viewWithTag:1]; 
    if (!background) { 
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
     background = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)]; 
     background.backgroundColor = [UIColor lightGrayColor]; 
     background.tag = 1; 
     [cell.contentView addSubview:background]; 

     nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(84.0f, 10.0f, 216.0f, 29.0f)]; 
     nameLabel.textColor = [UIColor blackColor]; 
     nameLabel.font = [UIFont boldSystemFontOfSize:20.0f]; 
     nameLabel.tag = 2; 
     [cell.contentView addSubview:nameLabel]; 

     dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(84.0f, 40.0f, 216.0f, 21.0f)]; 
     dateLabel.textColor = [UIColor blackColor]; 
     dateLabel.font = [UIFont systemFontOfSize:10.0f]; 
     dateLabel.tag = 3; 
     [cell.contentView addSubview:dateLabel]; 

     imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20.0f, 13.0f, 56.0f, 51.0f)]; 
     imageView.tag = 4; 
     [cell addSubview:imageView]; 
    } else { 
     nameLabel = (UILabel *) [cell viewWithTag:2]; 
     dateLabel = (UILabel *) [cell viewWithTag:3]; 
     imageView = (UIImageView *) [cell viewWithTag:4]; 
    } 

    nameLabel.text = self.nameArray[indexPath.row]; 
    dateLabel.text = self.dateArray[indexPath.row]; 
    imageView.image = [UIImage imageNamed: self.imageArray[indexPath.row]]; 

    return cell; 

} 
0

messageFeed :이있는 tableview의 모든 시간을 볼 할당 세포 feedView 이전 피드보기에서 추가의 tableview에서 사용을 다시하는 경우,이 재사용 가능한 코드를 시도

여기 내 코드입니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSString *CellIdentifier = @"newFriendCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(!cell) 
     { 
     // Configure the cell... 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     UIView *feedView = [[UIView alloc] init]; 
     feedView.tag=1; 
     UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(..., ..., ..., ...)]; 
     nameLabel.tag=2; 

     UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(..., ..., ..., ...)]; 
     dateLabel.tag=3; 

     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(..., ..., ..., ...)]; 
     imageView.tag=4; 

     [feedView addSubview:nameLabel]; 
     [feedView addSubview:dateLabel]; 
     [feedView addSubview:imageView]; 
     [cell addSubview:feedView]; 
    } 
     UIView *feedView=(UIView*)[cell viewWithTag:1]; 
     UILabel *nameLabel =(UILabel*)[feedView viewWithTag:2]; 
     UILabel *dateLabel =(UILabel*)[feedView viewWithTag:3]; 
     UIImageView *imageView =(UIImageView*)[feedView viewWithTag:4]; 
     nameLabel.text = self.nameArray[indexPath.row]; 
     dateLabel.text = self.dateArray[indexPath.row]; 
     imageView.image = [UIImage imageNamed: self.imageArray[indexPath.row]]; 
     return cell; 
    } 
+0

감사합니다. @ NANNAV하지만 작동하지 않았습니다. 그것은 feedView가 사용되지 않는 변수이고 빈 셀이있는 테이블을로드했다. – jsollo2

+0

죄송합니다. 제 실수는 내 편집 된 ans입니다. feedView 셀을 변경하는 것을 잊지 오전. – NANNAV

+0

@NANNV가이 작업을 프로젝트에서 수행합니까? 나는 그것을 시험해 보았고 빈 셀이있는 테이블을 얻고있다. 올바른 셀 수, 모두 빈 것입니다. – jsollo2

관련 문제