2011-03-08 2 views
0

iPad 앱용 테이블보기 컨트롤러를 만들고 있습니다. 이보기는 전체 화면 또는 모달보기 내에 표시 될 수 있으므로 크기가 표시 될 때마다 다를 수 있습니다. 나는 그것이 표시되는 크기에 상관없이 작업 할 수 있도록 제네릭 코드를 작성하는 것이 이상적입니다. (학문적 연습으로 나는 아이폰에서도 작업 할 수있는 코드를 원합니다.하지만 여기서는 실제로 요구 사항이 아닙니다.)상수를 사용하지 않고 그룹화 된 UITableViewCell 콘텐츠 영역 너비를 얻는 방법

그룹화 된 테이블 뷰 스타일을 사용하고 있습니다. UITextField를 셀의 뷰에 포함하려고합니다. 셀 필드에 텍스트 필드 (cell.AddSubview 사용)를 넣을 수 있지만 그룹화 된 스타일을 사용하면 텍스트 필드가 테이블의 왼쪽에 있습니다. 흰색 영역에 있어야하는 위치가 아닙니다.

주위를 둘러 보았습니다 (예 : UICatalog 샘플 및 여기 답변).이 문제에 대한 모든 해결책은 경계 영역의 상수 x 오프셋을 하드 코딩해야하는 것처럼 보입니다. 이 x 오프셋은 iPad에서 약 35px이지만 iPhone에서는 약 20px입니다.

더 나은 방법이 있어야한다고 생각하지만 아직 찾지 못했습니다. 직사각형 셀을 살펴 보았습니다. 경계선, 셀. 프레임, 셀. 내용보기. 경계선, 셀. 내용보기. 프레임 - 그룹화 된 셀의 실제 내용 영역이 없습니다.

다른 사람이 제안을했거나 값을 하드 코딩해야합니까?

감사합니다.

+0

셀을 직접 입력하지 않고 텍스트 필드를 'contentView'셀에 추가하려고합니다. 그것이 오작동하는 경우, 우리는 몇 가지 코드를 볼 필요가있다. 펜촉에 텍스트 필드를 설정하면 어떻게됩니까? – rgeorge

답변

1

나는 비슷한 대답을 찾고 있습니다.

"좋은"답변을 아직 찾지 못했지만이 대화는 contentView 프레임에 대한 호출이 실제 프레임에서 액세서리의 전용 영역과 둥근 가장자리 패딩을 반환하지 않는 이유에 대해 설명합니다. :

UITableViewCell's contentView's width with a given accessory type

나는 바렛의 조언을 촬영 그냥 하드 코딩 값으로 따라 프레임을 조정,하지만 당신은 더 나은 해답을 찾아내는 경우에 매우 관심이있을 것이라고했습니다.

6

UILabelUITextField으로 cell를 생성하는 샘플 임의 UIViewscell.contentView에 추가하고이 뷰 여기

위한 autoResizeMask 속성을 설정

// custom cell implementation 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 

    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
    { 
     self.label = [[[UILabel alloc] init] autorelease]; 
     self.label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17]; 
     self.label.backgroundColor = [UIColor clearColor]; 
     [self.contentView addSubview:self.label]; 

     self.textField = [[[UITextField alloc] init] autorelease]; 

     //this will make our textField resized properly 
     self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

     self.textField.borderStyle = UITextBorderStyleNone; 
     self.textField.backgroundColor = [UIColor clearColor]; 
     self.textField.textColor = [UIColor darkGrayColor]; //text color 
     self.textField.font = [UIFont systemFontOfSize:17.0]; //font size 
     self.textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 

     self.textField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard 
     self.textField.returnKeyType = UIReturnKeyNext; // type of the return key 

     self.textField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right 

     [self.contentView addSubview:self.textField]; 
     self.selectionStyle = UITableViewCellSelectionStyleNone; 
     self.accessoryType = UITableViewCellAccessoryNone; 
    } 
    return self; 
} 

프로그램과 데이터 소스의 방법은

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

그와 비슷한 것

  CustomCell* c = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (c == nil) 
     { 
      c = [[[CellWithPass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease]; 
      c.textField.delegate = self; 
     } 
     //in this method cell and cell subviews are not resized, so 
     //we are setting subviews frames here for cell with frame {{0,0},{320,44}} 
     c.label.text = @"label"; 
     float w = [c.label.text sizeWithFont:c.label.font].width; 
     c.label.frame = CGRectMake(10, 0, w, 44); 
     c.textField.frame = CGRectMake(w + 10, 12, c.contentView.frame.size.width - w - 20, 20); 
     return c; 
관련 문제