2010-10-11 67 views
0

我試圖創建一個ImageView的,按鈕,TextView的自定義單元格和標籤自定單元的UITableViewCell

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

,它只是在我身上退出。請有人讓我知道我可能做錯了嗎?

謝謝!

+3

呃,我不想聽起來很愚蠢或者什麼,但是這兩個代碼塊如何以編程方式相互關聯?我看不到任何常見變量,使用引用,設置屬性等... – Toastor 2010-10-11 16:24:31

+0

您可能想查看Apple的'UICatalog'示例代碼。 – Nimrod 2010-10-11 17:21:59

回答

1

你到底想要做什麼?

-viewDidLoad -method屬於自定義UITableViewCell

如果是這樣,它可能會崩潰,因爲您沒有名爲listData的屬性。

(另外,這會泄露像地獄,因爲你沒有釋放性能img, btn, textlabel,更重要的是,viewDidLoad中,方法應該總是開始與[super viewDidLoad](感謝@Toastor

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

+1

更不用說''[super viewDidLoad]'應該是您在實現viewDidLoad時調用的第一件事... – Toastor 2010-10-11 16:33:21

相關問題