2014-01-24 2 views
0

텍스트 필드가 채워진 테이블 셀을 만들려고합니다. 그러나 텍스트 필드의 수는 고정 된 금액이 아닙니다. 나는 배열을 테이블 셀로 쏘아서 나머지 부분을 그곳에서 처리 할 것이라고 생각했습니다.iOS : 배열을 추가하여 동적 셀 만들기

그러나 이것을 수행하는 방법을 알아낼 수 없습니다 ... 이상적으로는 UITableViewCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier의 필요한 데이터가있는 배열에 액세스 할 수 있습니다. 그런 다음 필요한만큼의 텍스트 필드를 반복하여 할당하십시오.

셀에 myArray 속성을 추가 한 다음 위 방법에서 alloc init을했지만 여전히 내보기 컨트롤러에서 액세스 할 수 없습니다. 예 : cell.myArray = ...

어떻게하면됩니까? 내가해야 할 더 좋은 방법이 있니? 이것은 단지 마음에 오는 첫 번째 방법입니다. 어떤 도움을 주셔서 감사합니다, 얘들 아.

EDIT (거친 예) :

//MyCell.h 

@interface ITDContactDetailsCell : UITableViewCell 
    @property (strong, nonatomic) NSMutableArray *myArray; 
@end 


//MyCell.m 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
     if (self) { 
      _myArray = [[NSMutableArray alloc]init]; 
      //Do some stuff with array (like add UITextViews) 
     } 
} 

//MyViewController.m 

- (UITableViewCell*)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = @"ContactDetails4LineCell"; 
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    //This doesn't even register 
    cell.myArray = [NSMutableArray arrayWithObjects: @"a", @"b", nil]; 

} 
+1

할당 된 위치는 어디에서 표시 할 수 있습니까? 할당 후 해당 배열에 액세스 할 수있는 위치를 표시 할 수 있습니까? – Mani

+0

나는 방금 예를 썼다. 모든 가져 오기가 정확하며 UIView와 같은 셀의 다른 항목에 액세스 할 수 있습니다. 그런 다음 다시 addSubview를 수행합니다. 배열이나 비슷한 버전이있는 경우에는 그렇지 않습니다. –

+1

나는 당신이 올바른 길에 있다고 생각합니다. 그러나 배열은 UI 요소가 아니라 메모리 내 구조에 불과하다는 것을 알고 있습니까? 사용자가 셀을 볼 수 있도록 배열을 사용하여 사용자 정의 셀에 달려 있습니다. 배열의 각 문자열에 대한 UILabel을 만들고이 레이블을 하위 뷰로 추가합니다. – danh

답변

1

ITDContactDetailsCell 클래스에 사용자가 직접 세터를 만들 수 있습니다. 다음 서명은 VC가 말할 때 호출되는 것입니다. cell.myArray = ...

- (void)setMyArray:(NSMutableArray *)array { 

    _myArray = array; // this + what ARC does to it is what the synthesized setter does 

    // here, we do something with the array so the user can interact with it 
    // labels are simpler, but textFields are the same idea 
    CGFloat x = 10, y = 10; 
    for (NSString *string in array) { 
     CGRect frame = CGRectMake(x,y,60,20); 
     UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
     label.text = string; 
     [self addSubview:label]; 
     x += 60; 
    } 
} 
1

당신은 흐름을 변경해야합니다. 때문에

  • 처음이라고합니다. 이며 empty array으로 인해 아무 것도 수행하지 않습니다.
  • cell.myArray = [NSMutableArray arrayWithObjects : @ "a", @ "b", nil]; 첫 번째 지점 이후에 두 번째 실행되므로 도 없습니다.

업데이트 :

이 시도 .. cell.h에서

cell.m

-(void)addTextField 
{ 
    // Do your stuff 
} 

에서

-(void)addTextField; 

귀하의 ViewController에서

- (UITableViewCell*)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = @"ContactDetails4LineCell"; 
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    //This doesn't even register 
    cell.myArray = [NSMutableArray arrayWithObjects: @"a", @"b", nil]; 
    [cell addTextField]; 

} 
+0

도움을 주셔서 감사합니다.하지만 Xcode 결함 일뿐입니다. 다시 시작했고 이제 cell.myArray에 액세스 할 수 있습니다 (예제는 거친 것입니다). –