Iphone 如何用不同的UITextField覆盖UITextField

Iphone 如何用不同的UITextField覆盖UITextField,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我在xib中定义了一个UITableViewCell。 此UITableViewCell有一个名为itemText的UITextField。 此UITableViewCell也以其他方式格式化 在表中的所有单元格中,我希望输入xib中定义的UITEXT字段 然而,在一个单元格中,我想使用另一个UITextField,它是我以编程方式定义的,名为FinanceTextField 在cellforindexpath方法中,我使用了以下行: cell.itemText = [[[FinanceTex

我在xib中定义了一个UITableViewCell。 此UITableViewCell有一个名为itemText的UITextField。 此UITableViewCell也以其他方式格式化

在表中的所有单元格中,我希望输入xib中定义的UITEXT字段

然而,在一个单元格中,我想使用另一个UITextField,它是我以编程方式定义的,名为FinanceTextField

在cellforindexpath方法中,我使用了以下行:

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

它不起作用?为什么?

创建两个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";
    }
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=nil;
//对于“成分”部分,如有必要,请创建一个新单元格,并为其配置一个额外的数量标签。为该单元格指定一个与其他部分中的单元格不同的标识符,以便它可以单独出列。
if(indexPath.section==配料部分){
NSU整数IngreditCount=[recipe.Components count];
NSInteger行=indexPath.row;
if(indexPath.row
这只是项目的一部分,展示了如何创建不同的标识符。从这里你可以自己得到所有的