2012-03-26 5 views
5

테이블보기가있는 iOS 앱이 있습니다. 행을 여러 번 선택하면 선택한 정보가 중복됩니다.UITableView에서 중복 된 텍스트

당신은 사진을 볼 수 있습니다 enter image description here

나는이 방법으로 세포의 추가

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

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Configure the cell... 
    UILabel *label = [[UILabel alloc] init]; 
    Place *item = [source objectAtIndex:indexPath.row]; 
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]]; 
    label.text =item.name; 
    label.frame = CGRectMake(5, 5, 310, 35); 
    [cell addSubview:label]; 
    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    OnePlaceViewController *placeView = [[OnePlaceViewController alloc] initWithNibName:@"OnePlaceViewController" bundle:nil]; 
    placeView.nom = indexPath.row; 
    placeView.source = source; 
    placeView.route = route; 
    placeView.titleView = titleView; 
    /* 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.selected = NO; 
    */ 
    [self.navigationController pushViewController:placeView animated:YES]; 
} 

왜 정보를 복제 할 수 있습니까? Thnx.

+3

나는 수정 구슬을 집에두고 왔기 때문에 코드를 게시해야합니다. –

+0

몇 가지 코드를 추가하십시오. 그것을 봐주십시오 –

+1

@EvanMulawski 권자 ...하지만 첫 번째 추측에, 그것은 세포 재사용 문제가있는 것 같다. 더 나은 대답을 위해서는'cellForRowAtIndex'와'didSelectRowAtIndex' 둘 다 필요합니다. – tipycalFlow

답변

7

나는 문제가 당신이 하위 뷰 매번로 UILabel을 추가하는 것입니다 생각합니다. 셀이 재사용되므로 테이블이 다시로드 될 때마다 UILabel이 여러 개 추가됩니다. 그래서, 제 제안은 새 레이블을 추가하기 전에 셀의 모든 하위 뷰를 검토하고 제거하는 것입니다.

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

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
// Configure the cell... 
UILabel *label = [[UILabel alloc] init]; 
Place *item = [source objectAtIndex:indexPath.row]; 
[label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]]; 
label.text =item.name; 
label.frame = CGRectMake(5, 5, 310, 35); 
for(UIView *v in [cell subviews]) 
{ 
    if([v isKindOfClass:[UILabel class]]) 
     [v removeFromSuperview]; 
} 
[cell addSubview:label]; 
return cell; 
} 
+0

정말로, 내 대답을 받아 들여야한다고 생각하지 않는다. 훨씬 깨끗한 접근 방식입니다. 항상 레이블을 추가, 제거 및 다시 만들 필요가 없습니다. 또한 DONT는 모든 하위 뷰를 거쳐야합니다. 내 솔루션은 메모리가 덜 강하고 빠릅니다. 그러나 자신을 선택하십시오. – calimarkus

+1

@ jaydee3 당신의 접근 방식이 내 것보다 훨씬 깨끗하고 효율적이라는 것은 의심의 여지가 없다. – tipycalFlow

+0

불쾌감은 없지만 SO는 지식 기반이며 항상 최상의 답변을 제공해야합니다. 대답은 그를위한 것이 아닙니다. – calimarkus

8

당신은 그런 식으로 변경 .. 라벨 매번 추가하는 :

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

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    // Configure the cell... 
    UILabel *label = [[UILabel alloc] init]; 
    Place *item = [source objectAtIndex:indexPath.row]; 
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]]; 
    label.frame = CGRectMake(5, 5, 310, 35); 
    label.tag = 666; 
    [cell addSubview:label]; 
    // add this unless you're using ARC 
    // [label release]; 
    // [cell autorelease]; 
    } 

    UILabel* cellLabel = (UILabel*)[cell viewWithTag: 666]; 
    cellLabel.text = item.name; 

    return cell; 
} 
관련 문제