自定义单元格UITableViewCell

时间:2010-10-11 16:16:06

标签: iphone objective-c uitableview

我正在尝试使用imageview,button,textview和标签

创建自定义单元格
#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

我在视图中称呼它:

- (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;
}

不幸的是,它只是退出了我。有人能让我知道我可能做错了吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

你到底想做什么?

-viewDidLoad - 方法是否属于自定义UITableViewCell

如果是这样,它可能会崩溃,因为您没有名为listData的属性。

(另外,这会像地狱一样泄漏,因为你没有发布属性img, btn, textlabel而且,更重要的是,viewDidLoad-method应始终[super viewDidLoad]开头(感谢 @Toastor

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