2014-07-26 4 views
3

50 행의 테이블 뷰 하나에 3 개의 사용자 정의 셀을 표시 할 수 있습니다. 내 필요를 충족시키는 참조를 찾았습니다. Multiple Custom Cells dynamically loaded into a single tableview팩터 리 패턴을 사용하여 동일한 테이블에 다른 사용자 정의 셀로드

세포에 대한 객체를 만드는 것이 복잡해 보입니다. 내 필요에 따라 3 개의 셀이 동일한 기능을 수행하지만보기가 다르다.

세포를 만들기 위해 공장 패턴을 사용할 수 있습니까?

이러한 패턴의 구현이 있습니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     // I would like to create object some like this 
     CustomCell *cell = factory.getCell("customCell1", tableView); 

} 

사용자 지정 셀에 대한 클래스 다이어그램이 있습니다. enter image description here

+0

스토리 보드를 사용하고 있습니까? – duci9y

+0

아니요, 스토리 보드를 사용하지 않고 xib를 사용하여 맞춤형 셀을 만듭니다. – satyanarayana

+0

수정 된 답변을 보셨나요? – arturdev

답변

3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell<CustomCellProtocol> *cell = [factory getCellForIndexPath:indexPath tableView:tableView]; 

    // Getting data for the current row from your datasource 
    id data = self.tableData[indexPath.row]; 
    [cell setData:data]; 

    return cell; 
} 

// 귀하의 공장 클래스입니다.

- (UITableViewCell<CustomCellProtocol> *)getCellForIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView 
{ 
    UITableViewCell<CustomCellProtocol> *cell; 
    NSString *cellNibName; 
    if (condition1) { 
     cellNibName = @"CustomCell1"; //Name of nib for 1st cell 
    } else if (condition2) { 
     cellNibName = @"CustomCell2"; //Name of nib for 2nd cell 
    } else { 
     cellNibName = @"CustomCell3"; //Name of nib for 3th cell 
    } 

    cell = [tableView dequeueReusableCellWithIdentifier:cellNibName]; 

    if (!cell) { 
     UINib *cellNib = [UINib nibWithNibName:cellNibName bundle:nil]; 
     [tableView registerNib:cellNib forCellReuseIdentifier:cellNibName]; 
     cell = [tableView dequeueReusableCellWithIdentifier:cellNibName]; 
    } 

    return cell; 
} 
+0

답장을 보내 주셔서 감사합니다 위의 코드는 식별자로 셀을 dequeuing하지만 동일한 방법으로 Xib에서 새 셀을 만들고 해당 셀을 대기열에서 제외하는 팩터 리를 만들고 싶습니다. – satyanarayana

+0

수정 된 답변 확인 – arturdev

+0

감사합니다. 도움이됩니다. – satyanarayana

1

사용자가 축약을 해제 할 수 없기 때문에 공장 방법이 적절하지 않습니다.

등록 테이블보기에서 다시 각 사용자 정의 클래스 (viewDidLoad이 작업을 수행 할 수있는 좋은 장소입니다) : cellForRowAtIndexPath에서

[self.tableView registerClass:[CustomCell1 class] forReuseIdentifier:@"customCell1"]; 
// Repeat for the other cell classes, using a different identifier for each class 

이있는 다음 디큐, 당신이 원하는 입력 운동 :

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
[cell setData:data]; // all subclasses can do this 

가능한 경우 새 셀이 만들어 지거나 풀에서 반환됩니다.

+0

답장을 보내 주셔서 감사합니다. factory에 tableView 참조를 보내면 팩토리에서 셀을 dequeue 할 수 있습니다. CustomCell * cell = factory.getCell ("customCell1", tableView); – satyanarayana

+1

@satyanarayana 공장 방법은이 작업을 수행하는 올바른 방법이 아닙니다. 이미 테이블 뷰에서 재사용 대기열 동작을 얻고 있습니다. – duci9y

+0

@ duci9y는 절대적으로 적합합니다. 테이블 뷰는 "공장"의 작업을 수행하고 재사용을 처리합니다. 프레임 워크와 싸우지 마십시오. – jrturton