2011-01-14 6 views
3

나는 현재 내 jQuery과 내 cellForRowAtIndexPath에 대한 다음과 같은 코드가 있습니다있는 UITableViewCell 내용보기 - 추가 UILabels

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

    static NSString* CellIdentifier = @"Cell"; 
    UILabel* nameLabel = nil; 
    UILabel* valueLabel = nil; 
    UILabel *percentLabel = nil; 

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    if (cell == nil) 
    { 
     inthere = YES; 
     cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault 
             reuseIdentifier: CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(7.0, 0.0, 140.0, 44.0)] autorelease]; 
     nameLabel.tag = 21; 
     nameLabel.font = [UIFont systemFontOfSize: 12.0]; 
     nameLabel.textAlignment = UITextAlignmentLeft; 
     nameLabel.textColor = [UIColor darkGrayColor]; 
     nameLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 
     nameLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview: nameLabel]; 

     valueLabel = [[[UILabel alloc] initWithFrame: CGRectMake(165.0, 0.0, 80, 44.0)] autorelease]; 
     valueLabel.tag = 22; 
     valueLabel.font = [UIFont systemFontOfSize: 11]; 
     valueLabel.textAlignment = UITextAlignmentRight; 
     valueLabel.textColor = [UIColor blueColor]; 
     valueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     valueLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview: valueLabel]; 

     percentLabel = [[[UILabel alloc] initWithFrame: CGRectMake(245, 0.0, 65, 44.0)] autorelease]; 
     percentLabel.tag = 24; 
     percentLabel.font = [UIFont systemFontOfSize: 12]; 
     percentLabel.textAlignment = UITextAlignmentRight; 
     percentLabel.textColor = [UIColor blueColor]; 
     percentLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     percentLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview: percentLabel]; 
    } 
    else 
    { 
     nameLabel = (UILabel*)[cell.contentView viewWithTag:21]; 
     valueLabel = (UILabel*)[cell.contentView viewWithTag:22]; 
     percentLabel = (UILabel *)[cell.contentView viewWithTag:24]; 
    } 

    ...and then I initialize the text of each of these three labels... 

}

하지만를 내가 뭘하고 싶은 세 가지 라벨 따라 색상이 다를 수 있습니다입니다 세포에. 예를 들어 섹션 3의 모든 셀에는 빨간색 이름 라벨이 있어야하고 섹션 5의 모든 셀에는 녹색 valueLabels이 있어야합니다. 나는 모든 레이블의 텍스트를 초기화 한 후 내가 코드에이를 삽입한다면 :

if(indexPath.section==3) { 
    nameLabel.textColor = [UIColor redColor]; 
} 
if(indexPath.section==5) { 
    valueLabel.textColor = [UIColor greenColor]; 
} 

는 모든 것이 엉망이됩니다 테이블 텍스트의 색상이 잘못되는 이상한 장소 및 라벨에있는과 함께, 모든 glitchy이며, 다소 임의적으로 보입니다.

테이블의 모든 단일 셀에 대해 세 라벨의 색상을 어떻게 지정합니까?

+0

는이 glitchyness의 스크린 샷을 공급할 수를 재설정 한? –

+0

아닙니다. 일종의 개인 정보입니다. 그러나 콘텐츠 조회수가 주변에서 섞여 있고 적절하게 정리되지 않았거나 그와 비슷한 것이 분명합니다. – CodeGuy

+0

텍스트가 이상한 장소에 있는지, 아니면 색상이 잘못 되었습니까? – Anna

답변

0

당신은 모두 라벨 '텍스트 색상에게 셀이

이 시도를 재활용 할 때마다,

if (indexPath.section == 3) { 

    nameLabel.textColor = [UIColor redColor]; 
    nameLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); 
    valueLabel.textColor = [UIColor blackColor]; 
    valueLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); 
} 

if (indexPath.section == 5) { 

    nameLabel.textColor = [UIColor blackColor]; 
    nameLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); 
    valueLabel.textColor = [UIColor greenColor]; 
    valueLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); 
} 
+0

잘 작동합니다. 그러나 레이블의 틀을 바꾸고 싶다면 어떻게해야할까요? 나는이 구조를 시도하고 프레임을 지정했지만 작동하지 않습니다. 물건들은 다시 무작위로 보인다. 내가 어떻게 할 수 있니? – CodeGuy

+0

내 코드를 수정했습니다. 당신이 그렇게하고 있는지 확인하십시오. 네가 그렇게한다면 그것은 효과가있다. – EmptyStack

0

코드가 괜찮은 것 같습니다. 시도 할 것을 제안 할 수있는 유일한 방법은 레이블에 autorelease을 사용하지 않고 contentView에 추가 한 직후 각각 release을 사용하는 것입니다.

0

UITableViewCells가 임의로 시스템에 의해 재활용되기 때문에 문제가 발생합니다.

if(indexPath.section==3) 
    nameLabel.textColor = [UIColor redColor]; 
else 
    nameLabel.textColor = [UIColor blackColor]; 

이 Btw은 휴대가 매우 복잡 보이는

, 나는 인터페이스 빌더와 함께 만드는 권하고 싶습니다 : 당신은 또한 세포의 기본 값을 설정해야하는 이유입니다.

+0

감사합니다. 이것은 잘 작동합니다. 이제 여러 섹션의 레이블 프레임을 변경하고 싶습니다. 나는이 같은 구조를 시도했으나 효과가 없었다. 내가 어떻게 할 수 있니? – CodeGuy

+0

"프레임 변경"이란 무엇을 의미합니까? 셀의 높이를 나타내는 경우 - (CGFloat) tableView : (UITableView *)를 확인하십시오. heightViewRowAtIndexPath : (NSIndexPath *) indexPath; 그러나 조심하십시오, 그것은 천천히 일 수 있습니다 (내 블로그 http://jomnius.blogspot.com/2011/02/uitableview-with-custom-cell-height.html에서 더 많은 정보) – JOM

관련 문제