Reputation: 2762
I have a UITableViewCell defined in a xib. This UITableViewCell has a UITextField, called itemText. This UITableViewCell is also otherwise formatted.
In all my cells in the table I would like to the UITextField as defined in the xib.
However, in one cell I would like to use a different UITextField, I defined programmatically, called FinanceTextField.
In the cellforindexpath method I used the following line:
cell.itemText = [[[FinanceTextField alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
It does not work? Why?
Upvotes: 3
Views: 149
Reputation: 5935
Create two UITableViewCell classes. One for your usual view, one that has a FinanceTextField. Load them both in from xibs. In your cellForIndexPath, determine which cell you want to use, and load (and reuse) the appropriate type. Even though only one cell uses the different cell, it will all work. In fact, you can have all different types of cells in the same table, all determinant on the row, such as the first row with regular label text, the second with a textfield, the third with a button, etc.
There's a sample project you can look to do this. The "Recipes" sample on the apple's developer website. Here's a part of the code you might be interested in :
- (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";
}
This is just a part of the project that shows how to create different identifiers. From here you can get it all by yourself...
Upvotes: 1