2012-12-09 2 views
0

영양 정보가있는 UITableView 테이블을 채 웁니다. 각 줄에는 영양소의 절대 양과 일일 백분율 값이 포함됩니다. 각 행의 왼쪽에 양을 정렬하고 오른쪽에 일일 비율 값을 정렬하여 정보가 더 깔끔하고 모든 값이 정렬되도록합니다. 내가 이것을 할 수있는 방법이 있습니까? 감사!Xcode에서 왼쪽과 오른쪽 부분의 텍스트 부분을 정렬합니다.

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NutritionCell" forIndexPath:indexPath]; 


    if(!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"NutritionCell"]; 
    } 


    CGRect oldFrame = cell.frame; 
    cell.textLabel.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, tableView.frame.size.width/2, oldFrame.size.height); 
    cell.detailTextLabel.frame = CGRectMake(oldFrame.origin.x + tableView.frame.size.width/2, oldFrame.origin.y, tableView.frame.size.width/2, oldFrame.size.height); 

    cell.textLabel.text = [factamount objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [percentDV objectAtIndex:indexPath.row]; 
    return cell; 

} 

답변

2

다음과 같은 코드를 사용할 수 있습니다,

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

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

    cell.textLabel.text = @"Left"; 
    cell.detailTextLabel.text = @"Right"; 

    return cell; 
} 

는 세포 (더 두 이상)에 여러 사용자 지정 라벨을 사용하려는 경우, 당신은뿐만 아니라 것을을하고 cell.contentView의 하위 뷰로 추가 할 수 있습니다 textAlignment 속성을 사용하여 정렬하십시오. 이러한 레이블이 적절한 위치에 표시되도록 프레임을 설정할 수 있습니다. 이 경우

당신은 또한 UITableViewCellStyleValue1하여이 작업을 수행 할 수

myLabel1.textAlignment = NSTextAlignmentLeft; 
myLabel2.textAlignment = NSTextAlignmentRight; 
+0

: 자동으로 셀에이 라벨을 추가하는 . – iDev

+0

흠 ... 아무것도 바뀌지 않는 것 같아요. – maximum411

+0

@ maximum411, 어떻게 프레임을 설정합니까? 그리고 설정 한 프레임은 무엇입니까? 왼쪽 라벨의 너비를 줄이고 확인 했습니까? 그렇지 않다면 새로운 라벨을 만들고 셀에 추가 할 수 있습니다. 그러나 가능하면이 캡쳐 화면을 게시하십시오. – iDev

2

로 할 필요가있다. ., 당신은 "cell.textLabel.frame는 = CGRectMake이 (..."`다른 라벨 동일한 작업을 수행`시도 할 수 있습니다 프레임을 설정하려면, maximum411 @

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *cellID = @"CELLID"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
     if(!cell) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]; 
     } 

     cell.textLabel.text = @"AMOUNT TEXT"; 
     cell.detailTextLabel.text = @"PERCENT TEXT"; 
     return cell; 
    } 
+0

이 코드를 사용했지만 지금은 cell.textlabel.text의 내용 만보고 있습니다. – maximum411

+0

This 이 코드를 사용하고 있습니까? 편집 된 질문에 위에 포함되어 있습니까? – maximum411

+0

뭔가 있습니까? 프레임 설정 코드와 관련이 있습니까? UITableViewCell이 자동으로 왼쪽과 오른쪽에 맞 춥니 다. 주석 처리하고 작동하는지 확인해보십시오. :) 새 프로젝트에서 코드를 테스트 한 결과, 어떻게 작동하는지 알 수있었습니다. – GracelessROB

관련 문제