views:

78

answers:

1

I am working on an in app purchase app. I would like fill a table view with a list of products, and put a BUY button on the right hand side of each entry. Does anyone have the UITableView - fu necessary to do this?

Thanks!

+1  A: 

Create a custom class that extends UITableViewCell

@interface CustomTblCell : UITableViewCell

In the corresponding NIB CustomTblCell.xib drag and drop UILabel and UIButton elements (side-by-side).

Finally, in your method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath initialize and return the CustomTblCell

Hope this helps!

Here is a code snippet for the cellForRowAtIndexPath: method I mentioned above

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CustomCellIdentifier = @"customCellIndentifier";

    CustomTblCell *cell = (CustomTblCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if(cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTblCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.label.text = @"My Product";
    return cell;
}

If you have many rows in your table such that the user has to scroll through the table use 'dequeueReusableCellWithIdentifier`. This optimizes the code to reuse the same customTblCell object which is now not in user view. The deque method takes a String as input arg (customTableCellIdentifier). Make sure this matches the Identifier string you specify in the IB while creating the CustomTblCell

Mihir Mathuria
Thanks. That works. My actual solution involved loading up a view controller with this nib: one line instead of two seemed more elegant.
Jacko