2012-05-23 3 views
-1

저는 iPhone 개발에 새로운입니다. 셀 내부에 레이블이있는 tableview가있는 응용 프로그램을 개발 중입니다. 나는 이것을 한 각 셀 행에 대해 이렇게 if(indexpath.row == 0)처럼 각 행마다 다른 레이블을 붙였다. 그러나 테이블 뷰를 스크롤 할 때 레이블이 섞여 있습니다. 제발 도와주세요! 당신이 cell== nil 상태를 확인하기 전에 cell=nil;을 넣어 또는 셀마다 시간을 만들 수 있습니다이 문제를 해결하기 위해내 tableview 셀 레이블을 제어하는 ​​방법?

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

if (indexPath.row == 2) 
{ 
    pendingListNumberLabel = [[UILabel alloc] init]; 
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 
    pendingListNumberLabel.textColor = [UIColor orangeColor]; 
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
    [cell addSubview:pendingListNumberLabel]; 
} 

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row]; 
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
return cell; 
+4

후 ... 당신 .... 위의 코드로! – borrrden

+0

더 자세한 설명과 일부 코드 wud는 우리가 U를 돕는 데 도움이됩니다. :) –

+0

코드를 보지 않고, 재사용 식별자를 부적절하게 사용하고 있다고 가정합니다. –

답변

0

UILabel을 하위보기로 추가하는 것은 실수입니다. 는 일반적으로 당신은 이런 종류의 물건에 대한 사용자 지정있는 UITableViewCell을 사용하지만, 빠른 수정이는 다음과 같습니다

if (indexPath.row == 2) 
{ 
    pendingListNumberLabel = [[UILabel alloc] init]; 
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 
    pendingListNumberLabel.textColor = [UIColor orangeColor]; 
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
    pendingListNumberLabel.tag = 1337 
    [cell addSubview:pendingListNumberLabel]; 
} else { 
    for (UIView * subView in [cell subviews]) { 
     if ([subView tag] == 1337) { 
      [subView removeFromSuperview]; 
     } 
    } 
} 
+0

덕분에 도움이되었습니다. – jone

-1

. 그것은 작동하지만 그것은 좋은 습관하지 않습니다. 이것은 또한

정적있는 NSString * CellIdentifier = @ "셀"텍스트에 대한 작업

;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier : CellIdentifier];

cell = nil;

경우 (셀 == 닐)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

}

경우 (indexPath.row == 2) {

pendingListNumberLabel = [[UILabel alloc] init]; 

pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 

pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 

pendingListNumberLabel.textColor = [UIColor orangeColor]; 

pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 

[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
[cell addSubview:pendingListNumberLabel]; 

}

cell.textLabel.text = [affiliationsArray objectAtIndex : indexPath.row];

[cell setAccessoryType : UITableViewCellAccessoryDisclosureIndicator];

복귀 셀;

0

당신이 올바른 방법으로 재사용 가능한 세포를 사용하지 않기 때문에 이런 일이 ..

당신의 코드 교체 -이와

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

-

UITableViewCell *cell = [[UITableViewCell alloc] init]; 

위의 코드를 문제를 해결할 수 있지만 셀 수가 많으면 좋지 않습니다. 이 경우 재사용 가능한 셀을 사용해야합니다.당신의 tableView:cellForRowAtIndexPath: 방법에서

+0

네가 제공 한이 셀과 동일한 셀을 사용하고 있다는 것입니다. 하지만 내가 서버에서 레이블 텍스트를 받고. 나는 웹 서비스를 위해 viwedidload에 코드를 작성했다. 이제 내 레이블 텍스트가 업데이트되지 않습니다. – jone

0

먼저 다음있는 tableView 행에 해당하는 NSArray에서 데이터를 잡고 셀의 textLabel라는의 text 속성에 할당 "행을"사용이

NSUInteger row = [indexPath row]; 

같은 선택한 행을 얻을 이

NSString *string = [NSString stringWithFormat:@"%@",self.allItems objectAtIndex:row]; 
cell.textLabel.text = string; 
return cell; 

같은 물론 당신은 이미이 같은 재사용 식별자와 UITableViewCell을 만든 것 :

static NSString *TableIdentifier = @"TableIdentifier"; 

UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:TableIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleValue1 
      reuseIdentifier:TableIdentifier]; 
} 

키가 테이블 행과 정확히 일치하는 배열을 사용하고 있습니다. (예 : Array의 항목 0은 tableView의 행 0에 해당합니다.) 그러면 잘못 갈 수 없습니다.

0
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
else 
     { 
      UILabel *titleLbl = (UILabel *)[cell viewWithTag:1]; 
      [titleLbl removeFromSuperview]; 
     } 

if (indexPath.row == 2) 
{ 
    pendingListNumberLabel = [[UILabel alloc] init]; 
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ; 
    pendingListNumberLabel.textColor = [UIColor orangeColor]; 
    pendingListNumberLabel.tag = 1;// Write here..................................... 
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30); 
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]]; 
    [cell addSubview:pendingListNumberLabel]; 
} 

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row]; 
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
return cell; 

나는 그것이 당신에게 도움이 될 것이라고 생각합니다.

0

당신은 단지 if (cell == nil) 검사를 생략하여 라벨이 혼합의 문제를 해결할 수 있습니다. 벨로우즈 코드를 사용

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
//if (cell == nil) // Need not this checking 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

를 사용하여 셀 할당 직접 :

관련 문제