La création d'un UITableView par programmation

J'ai une application dans Xcode 4.6 qui utilise les storyboards. J'ai ajouté une UITableView de vue de la classe contrôleur, qui a fonctionné comme prévu. Cependant, quand j'ai essayé de la suppression de la UITableView dans le storyboard et l'ajouter de nouveau dans la même classe de la programmation, j'ai rencontré deux problèmes spécifiques:

1) Bien que j'ai mis le UITableViewCell type de type de sous-titres, les détails de l'étiquette n'apparaît plus.

2) La séquence qui devrait se produire lorsque je sélectionne une cellule ne se produit pas, et préparer la transition n'est même pas d'être appelé, indiquant qu'aucun message n'est envoyé à la vue de la table lorsqu'une cellule est sélectionnée.

Voici le code correspondant:

@interface StatsTableViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@end
@implementation StatsTableViewController
-(UITableView *)makeTableView
{
CGFloat x = 0;
CGFloat y = 50;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height - 50;
CGRect tableFrame = CGRectMake(x, y, width, height);
UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
tableView.rowHeight = 45;
tableView.sectionFooterHeight = 22;
tableView.sectionHeaderHeight = 22;
tableView.scrollEnabled = YES;
tableView.showsVerticalScrollIndicator = YES;
tableView.userInteractionEnabled = YES;
tableView.bounces = YES;
tableView.delegate = self;
tableView.dataSource = self;
return tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [self makeTableView];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newFriendCell"];
[self.view addSubview:self.tableView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) 
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Friend *friend = [self.fetchedResultsController objectAtIndexPath:indexPath];
**//THIS DATA APPEARS**
cell.textLabel.text = friend.name;
cell.textLabel.font = [cell.textLabel.font fontWithSize:20];
cell.imageView.image = [UIImage imageNamed:@"icon57x57"];
**//THIS DATA DOES NOT APPEAR**
cell.detailTextLabel.text = [NSString stringWithFormat:@"%i Games", friend.gameCount];
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"detailsView" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//I set the segue identifier in the interface builder
if ([segue.identifier isEqualToString:@"detailsView"])
{
NSLog(@"segue"); //check to see if method is called, it is NOT called upon cell touch
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
///more code to prepare next view controller....
}
}

Je ne suis pas sûr de ce que je suis en oubliant de le faire pour que je puisse résoudre ces deux problèmes. Toute aide est appréciée.

  • Essayez de définir la hauteur de la cellule.
  • n'est-ce pas la hauteur de ligne la hauteur de la cellule? je l'ai réglé à 45 qui devrait être plus assez de place...
  • Oh oui ce serait, je pense que vous avez défini une mauvaise méthode de didDeselect il devrait plutôt être didSelect.
InformationsquelleAutor jac300 | 2013-04-06