Suggestions to manage different contents for a custom UITableViewCell

140 views Asked by At

In my application I present a UITableViewController with custom UITableViewCells. Each cell has been created throught .xib file. I decided to take this way because each cell displays different elements. Elements are a UILabel, a UITextField and a UIImageView. Each cell displays info about products.

The code I use to present the custom cell is the following:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:kCellTableIdentifier];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];     
        cell = self.customTableViewCellOutlet;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundView = [[[BackgroundCell alloc] init] autorelease];
        cell.selectedBackgroundView = [[[BackgroundCell alloc] init] autorelease];
    }

    // configure the cell with data
    [self configureCell:cell atIndexPath:indexPath];    

    return cell;
}

where self.customTableViewCellOutlet is an outlet to the cell .xib file with an assign retain policy.

The code works very well.

Now I need to implement the following two specifictions:

1) First, when the user taps in a UITextField, that element becomes editable. When the user taps outside the modified value is saved and the UITextField becomes not editable.

For this first specification I thought about the following mechanism. The File's owner for my .xib file could be UITableViewController. Doing this, my UITableViewController could implement UITextFieldDelegate and listen for UITextField events. The only aspect that is not clear for me is how to listen when the user taps outside the UITextField.

2) When the user tap in a row, the cell has to change its content. In particular, each cell has to change its content. From displaying a product (good) to presenting a detailed description for that product. In other words, when the user tap the cell I need tho swicth from a view (that one that displays raws info) to another one.

Here I don't know how to proceed.

Could you provide me some suggestions? Thank in you in advance.

1

There are 1 answers

0
vishy On

implementing the textField Delegate in UITableViewController or UITableViewCell depends on the requirements. wether u wanted the written text to be in array? then u can set index as tag of textField. Then get the text in delegate and add to the array then reload the table. Another thing u can add a tap gesture on the cell, which returns u the row, when tapped outside the textField..