2011-11-27 3 views
3

xib에 UITableViewCell이 정의되어 있습니다. 이 UITableViewCell에는 itemText라는 UITextField가 있습니다. 이 UITableViewCell도 형식이 지정되지 않습니다.UITextField를 다른 UITextField로 덮어 쓰는 방법

테이블의 모든 셀에서 xib에 정의 된대로 UITextField를 사용하고 싶습니다.

그러나 하나의 셀에서 FinanceTextField라는 프로그래밍 방식으로 정의 된 다른 UITextField를 사용하고 싶습니다.

cell.itemText = [[[FinanceTextField alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease]; 

그것은 작동하지 않습니다 나는 다음과 같은 라인을 사용 cellforindexpath 방법에

? 왜?

답변

1

두 개의 UITableViewCell 클래스를 만듭니다. 하나는 일반적인보기 용이고 하나는 FinanceTextField가 있습니다. xib에서 양쪽을로드하십시오. cellForIndexPath에서 사용할 셀을 결정하고 해당 유형을로드 (및 재사용)하십시오. 하나의 셀만 다른 셀을 사용하지만 모든 셀이 작동합니다. 실제로 동일한 표에 모든 유형의 셀을 가질 수 있습니다. 행의 모든 ​​결정자 (예 : 일반 레이블 텍스트의 첫 번째 행, 두 번째 텍스트 필드, 세 번째는 버튼 포함)

이 작업을 수행 할 수있는 샘플 프로젝트가 있습니다. 사과 개발자 웹 사이트의 "요리법"샘플. 여기에 관심이있을 수있는 코드의 일부는 다음과 같습니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = nil; 

// For the Ingredients section, if necessary create a new cell and configure it with an additional label for the amount. Give the cell a different identifier from that used for cells in other sections so that it can be dequeued separately. 
if (indexPath.section == INGREDIENTS_SECTION) { 
    NSUInteger ingredientCount = [recipe.ingredients count]; 
    NSInteger row = indexPath.row; 

    if (indexPath.row < ingredientCount) { 
     // If the row is an ingredient, configure the cell to show the ingredient name and amount. 
     static NSString *IngredientsCellIdentifier = @"IngredientsCell"; 
     cell = [tableView dequeueReusableCellWithIdentifier:IngredientsCellIdentifier]; 
     if (cell == nil) { 
      // Create a cell to display an ingredient. 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IngredientsCellIdentifier] autorelease]; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
     Ingredient *ingredient = [ingredients objectAtIndex:row]; 
     cell.textLabel.text = ingredient.name; 
     cell.detailTextLabel.text = ingredient.amount; 
    } else { 
     // If the row is not an ingredient the it's supposed to add an ingredient 
     static NSString *AddIngredientCellIdentifier = @"AddIngredientCell"; 
     cell = [tableView dequeueReusableCellWithIdentifier:AddIngredientCellIdentifier]; 
     if (cell == nil) { 
     // Create a cell to display "Add Ingredient". 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddIngredientCellIdentifier] autorelease]; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
     cell.textLabel.text = @"Add Ingredient"; 
    } 

이 서로 다른 식별자를 만드는 방법을 보여줍니다 프로젝트의 단지 부분이다. 여기에서 직접 모든 것을 얻을 수 있습니다 ...

관련 문제