views:

60

answers:

1

Hi,

I am trying to create a custom cell with an imageview, button, textview, and a label

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell {
    IBOutlet UILabel *nameLabel;
    IBOutlet UITextView *inputText;
    IBOutlet UIImageView *image;
    IBOutlet UIButton *btnBuy;
}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UITextView *inputText;
@property (nonatomic, retain) IBOutlet UIImageView *image;
@property (nonatomic, retain) IBOutlet UIButton *btnBuy;

@end

and I am calling this in a view:

- (void)viewDidLoad {
    UIImageView *img = [[UIImageView alloc] init];
    UIButton *btn = [[UIButton alloc] init];
    UITextView *text = [[UITextView alloc] init];
    UILabel *label = [[UILabel alloc] init];

    NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:img, btn, text, label, nil];

    NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
    self.listData = array;

    [row1 release];
    [array release];

    [super viewDidLoad];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

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

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
    }

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.listData objectAtIndex:row];
    cell.nameLabel.text = [rowData objectForKey:@"Name"];
    cell.textLabel.text = [listData objectAtIndex:row];
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath{
    NSUInteger row = [indexPath row];
    if (row == 0)
        return nil;
    return indexPath;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *rowValue = [listData objectAtIndex:row];
    NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selection" message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [alert show];
    [message release];
    [alert release];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kTableViewRowHeight;
}

Unfortunately, it simply exits on me. Can somebody let me know what I may be doing wrong please?

Thanks!

+1  A: 

What on earth are you trying to do?

Does the -viewDidLoad-method belong to the custom UITableViewCell?

If so, it is probably crashing because you don't have a property called listData.

(Additionally, this will leak like hell because you aren't releasing the properties img, btn, textandlabel. AND, more importantly, the viewDidLoad-mehtod should always start with [super viewDidLoad] (thanks to @Toastor))

Take a look at this guide on how to make a custom UITableViewCell.

Emil
not to mention that `[super viewDidLoad]` should be the first thing you call in your implementation of viewDidLoad...
Toastor