2012-05-31 3 views
0

Я кодировал пример UITableView с пользовательским UITableViewCell (PlayerCell). Все работает нормально, я могу видеть элементы в моей таблице, и я могу их выбрать, но когда я прокрутку вверх, приложение вылетает. Я просмотрел идентификатор, класс контроллера, все, но когда я использую пользовательскую ячейку, он не работает. Если я использую стиль по умолчанию, он отлично работает, включив прокрутку вверх.StoryBoard iOS 5 - пользовательский UITableViewCell Crash Scroll up

Я думаю, что у меня есть проблемы в этом методе: didSelectRowAtIndexPath

Но отладчик, не показывайте мне ошибку, только что-то вроде: EXEC BAD адрес; (я не имею компьютер здесь)

Это мой код:

MyTeamViewController.h

#import <UIKit/UIKit.h> 

@interface MyTeamViewController : UITableViewController 

@property (nonatomic, strong) NSMutableArray *players; 

@end 

MyTeamViewController.m

#import "MyTeamViewController.h" 
#import "Player.h" 
#import "PlayerCell.h" 



@interface MyTeamViewController() 

@end 

@implementation MyTeamViewController 
@synthesize players; 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 

    // Override point for customization after application launch. 
    players = [NSMutableArray arrayWithCapacity:20]; 
    Player *player = [[Player alloc] init]; 
    player.name = @"Bill Evans"; 
    player.game = @"Tic-Tac-Toe"; 
    player.rating = 4; 
    [players addObject:player]; 
    player = [[Player alloc] init]; 
    player.name = @"Oscar Peterson"; 
    player.game = @"Spin the Bottle"; 
    player.rating = 5; 
    [players addObject:player]; 
    player = [[Player alloc] init]; 
    player.name = @"Dave Brubeck"; 
    player.game = @"Texas Hold’em Poker"; 
    player.rating = 2; 
    [players addObject:player]; 



    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    //#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    NSUInteger numberCells = players.count;  

    return numberCells; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"PlayerCell"; 

    PlayerCell *cell = (PlayerCell *)[tableView 
             dequeueReusableCellWithIdentifier:CellIdentifier];  

    if(cell == nil) 
    { 
     NSLog(@"Cell is NIL"); 
     //I don't know what put here <-- MY PROBLEM!!! :(

    } 

    Player *player = [self.players objectAtIndex:indexPath.row]; 
    cell.nameLabel.text = player.name; 
    cell.pointsLabel.text = player.game; 
    cell.clubImageView.image = [self 
           imageForRating:player.rating]; 


    // Configure the cell... 
    return cell; 


} 

- (UIImage *)imageForRating:(int)rating 
{ 
    switch (rating) 
    { 
     case 1: return [UIImage imageNamed:@"1StarSmall.png"]; 
     case 2: return [UIImage imageNamed:@"2StarsSmall.png"]; 
     case 3: return [UIImage imageNamed:@"3StarsSmall.png"]; 
     case 4: return [UIImage imageNamed:@"4StarsSmall.png"]; 
     case 5: return [UIImage imageNamed:@"5StarsSmall.png"]; 
    } 
    return nil; 
} 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the specified item to be editable. 
return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
// Delete the row from the data source 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 
else if (editingStyle == UITableViewCellEditingStyleInsert) { 
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the item to be re-orderable. 
return YES; 
} 
*/ 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
    */ 
} 

@end 

Другие файлы:

Player.h

#import <Foundation/Foundation.h> 

@interface Player : NSObject 
@property (nonatomic, copy) NSString *name; 
@property (nonatomic, copy) NSString *game; 
@property (nonatomic, assign) int rating; 
@end 

Player.m

#import "Player.h" 

@implementation Player 

@synthesize name; 
@synthesize game; 
@synthesize rating; 
@end 

PlayerCell.h

#import <UIKit/UIKit.h> 

@interface PlayerCell : UITableViewCell 

@property (nonatomic, strong) IBOutlet UILabel *nameLabel; 
@property (nonatomic, strong) IBOutlet UILabel *pointsLabel; 
@property (nonatomic, strong) IBOutlet UIImageView 
*clubImageView; 

@end 

P layerCell.m

#import "PlayerCell.h" 

@implementation PlayerCell 

@synthesize nameLabel; 
@synthesize pointsLabel; 
@synthesize clubImageView; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

Большое спасибо!

EDIT: Я решил его: players = [[NSMutableArray arrayWithCapacity:20] retain];

+0

PlayerCell в раскадровке имеет corretly присвоен идентификатор и класс ... :( – chemitaxis

+0

Хорошо, я собираюсь попробовать этот учебник: http://www.techotopia.com/index.php/Using_Xcode_Storyboards_to_Build_Dynamic_TableViews_with_Prototype_Table_View_Cells – chemitaxis

ответ

0

Я хотел бы попробовать удалить

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

В PlayerCell.m - и их не нужно.

+0

Хорошо, я попробую, когда приду домой, спасибо! Я дам вам обратную связь! – chemitaxis

Смежные вопросы