UITableView programmgesteuert erstellen
Ich habe eine Anwendung in Xcode 4.6, die verwendet storyboards. Ich habe eine UITableView, um ein view-controller-Klasse, die funktioniert wie erwartet. Jedoch, wenn ich habe versucht, das löschen der UITableView in das storyboard und das hinzufügen es wieder in die gleiche Klasse programmgesteuert, ich lief in zwei spezifische Probleme:
1) Obwohl ich die UITableViewCell Typ Typ Untertitel ein detail, das label wird nicht mehr angezeigt.
2) Der Wechsel stattfinden soll, wenn ich wählen Sie eine Zelle nicht auftreten, und bereiten segue ist überhaupt noch nicht genannt, was darauf hinweist, dass keine Nachricht geschickt wird, um die Tabelle anzeigen, wenn eine Zelle ausgewählt ist.
Hier ist der relevante code:
@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....
}
}
Ich bin nicht sicher, was ich vergessen habe zu tun, so dass ich die Lösung dieser zwei Probleme. Jede Hilfe ist willkommen.
InformationsquelleAutor der Frage jac300 | 2013-04-06
Du musst angemeldet sein, um einen Kommentar abzugeben.
Beim registrieren der Klasse, und verwenden Sie dequeueReusableCellWithIdentifier:forIndexPath: die dequeue-Methode ist gewährleistet, um wieder eine Zelle, so if (cell == nil) - Klausel ist nie eingetragen. So, es einfach zu tun die alte Weise, nicht registrieren Sie die Klasse, und verwenden Sie dequeueReusableCellWithIdentifier:
Als für den übergang, es kann nicht aufgerufen werden, da kann man nicht machen einen übergang zu einer Tabelle, die Sie erstellt haben, in code, nicht in IB. Wieder zurück zu den alten Weg und nutzen tableView:didSelectRowAtIndexPath: wird aufgerufen, wenn Sie eine Zelle auszuwählen. Instanziieren Sie Ihre detail-controller und die trasition im code.
Nachdem edit:
Ich sah nicht, Ihren code Hinzugefügt, es. Sie implementiert haben didDeselectRowAtIndexPath eher als didSelectRowAtIndexPath. Wenn du das nicht ändern, Ihre segue arbeiten sollte.
InformationsquelleAutor der Antwort rdelmar
Könnten Sie tun, dass seine arbeiten 100% .
InformationsquelleAutor der Antwort Sumit singh
InformationsquelleAutor der Antwort iOS Lifee
InformationsquelleAutor der Antwort Parv
Beispieltabelle
pragma mark - UiTableview Datasource
.h
InformationsquelleAutor der Antwort sasidharan.M
vc.m
importieren "secondViewController.h"
vc.m
sv.m
sv.h
InformationsquelleAutor der Antwort sasidharan.M
Erstellen einer tableview mit tableViewController .
InformationsquelleAutor der Antwort Ashwin Felix
diese sind UITableView delegate-Methoden.
InformationsquelleAutor der Antwort soorej babu
InformationsquelleAutor der Antwort Acharya Ronak