2011-07-06 4 views
0

셀 세부 텍스트를 중앙에 배치하려고합니다.detailTextLabel textalignment

나는 모든 종류의 게시물을 읽었지만 이전 버전의 IOS에 대한 이야기를하고있는 것 같습니다. 나는 게시물의 모든 조합을 시도했지만 그것이 작동하게 만드는 행운이 없다고 생각합니다.

[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter]; 

나는 이것을 willDisplayCell과 아래 코드에서 시도했지만 어느 것도 작동하지 않습니다. 두 가지 방법으로 시도해 보았습니다.

이것이 작동하는지 또는 내 센터 기능 (방법)을 만들어야하는지 아는 사람이 있습니까?

static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];  
    } 


    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0]; 


    cell.detailTextLabel.font = [UIFont systemFontOfSize:16]; 


    NSMutableDictionary *curRow = [myData objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [curRow objectForKey:@"Description"]; 

    cell.detailTextLabel.text = [curRow objectForKey:@"Stats"]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 

답변

2

맞춤이 문제가있는 경우 맞춤 라벨을 만들고 하위 뷰를 셀에 추가 할 수 있습니다.

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

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     label = [[[UILabel alloc] initWithFrame:CGRectMake(55, 4, 260, 20)] autorelease]; 
     //make Your alignments to this label 
     label.font = [UIFont boldSystemFontOfSize:15.0]; 
     label.tag=25; 

     //make Your alignments to this detail label 
     detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(55, 25, 260, 15)] autorelease]; 
     detailLabel.font = [UIFont systemFontOfSize:13.0]; 
     detailLabel.tag=30; 
     [cell.contentView addSubview:label]; 
     [cell.contentView addSubview:detailLabel]; 
    } 
    else 
    { 
     label = (UILabel *)[cell.contentView viewWithTag:25]; 
     detailLabel = (UILabel *)[cell.contentView viewWithTag:30]; 
    } 
    label.text =[curRow objectForKey:@"Description"]; 
    detailLabel.text=[curRow objectForKey:@"Stats"]; 
    return cell; 
} 
+0

자세한 답변을 주셔서 감사합니다. –

+0

나는 또한 세 번째 하위 뷰를 만들 수 있습니까? 아니면 스타일이 그만합니까? –

+0

셀을 사용자 지정할 때 스타일에 대해 걱정할 필요가 없습니다. 원하는대로 할 수 있습니다. – iPrabu

2

또는 당신은 여전히 ​​자동 수직 중심을 활용하면서 detailTextLabel을 중심하려는 경우 - (void) layoutSubviews를 재정의해야합니다 (textLabel라는 수직으로 detailTextLabel이 비어있는 경우, 중심됩니다).

그렇지 않으면 레이블의 크기가 내용에 맞을 것이므로 textAlignment = UITextAlignmentCenter은 작동하지 않습니다.

- (void) layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height); 
    self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height); 
} 
관련 문제