Пользовательская ячейка UITableViewCell - PullRequest
0 голосов
/ 11 октября 2010

Я пытаюсь создать пользовательскую ячейку с изображением, кнопкой, текстом и меткой

#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 Ответ

1 голос
/ 11 октября 2010

Что, черт возьми, вы пытаетесь сделать?

Относится ли -viewDidLoad -метод к обычаю UITableViewCell?

Если это так, то этовозможно, сбой, потому что у вас нет свойства с именем listData.

(Кроме того, это будет чертовски трудно, потому что вы не освобождаете свойства img, btn, text и label. AND,что еще более важно, метод viewDidLoad должен всегда начинаться с [super viewDidLoad] (благодаря @ Toastor ) )

Взгляните на эторуководство о том, как сделать кастом UITableViewCell.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...