ここに再び ViewControllers があります。
ここに私のViewController.hがあります:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (copy, nonatomic) NSDictionary *firstTableView;
@property (copy, nonatomic) NSArray *firstTableViewKey;
@property (weak, nonatomic) IBOutlet UILabel *norskLabel;
@property (weak, nonatomic) IBOutlet UILabel *infinitivLabel;
@property (weak, nonatomic) IBOutlet UILabel *presensLabel;
@property (weak, nonatomic) IBOutlet UILabel *preteritumLabel;
@property (weak, nonatomic) IBOutlet UILabel *perfektumLabel;
@end
これが私のViewController.mです:
#import "ViewController.h"
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SectionsTableIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:@"SterkeVerb" ofType:@"plist"];
self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];
self.firstTableViewKey = [[self.firstTableView allKeys] sortedArrayUsingSelector:@selector(compare:)];
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.firstTableViewKey count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = self.firstTableViewKey[section];
NSArray *nameSection = self.firstTableView[key];
return [nameSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.firstTableViewKey[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];
cell.textLabel.text = nameSection[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0){
_norskLabel.text = @"å bake";
_infinitivLabel.text = @"zu backen";
_presensLabel.text = @"bäckt/backt";
_preteritumLabel.text = @"backte";
_perfektumLabel.text = @"hat gebacken";
}
else if (indexPath.row == 1){
_norskLabel.text = @"å motta";
_infinitivLabel.text = @"zu empfangen";
_presensLabel.text = @"empfängt";
_preteritumLabel.text = @"empfing";
_perfektumLabel.text = @"hat empfangen";
}
}
わかりましたので、テーブル ビューをセクション (AZ) に分割しました。コードを入力すると、最初のセクションで完全に機能しました。しかし、次のセクションのセルの 1 つを押すと、最初のセクションの最初のセルと同じ情報が表示されました。なぜ?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0){
_norskLabel.text = @"å bake";
_infinitivLabel.text = @"zu backen";
_presensLabel.text = @"bäckt";
_preteritumLabel.text = @"backte";
_perfektumLabel.text = @"hat gebacken";
else if (indexPath.row == 1){
_norskLabel.text = @"å begynne";
_infinitivLabel.text = @"zu beginnen";
_presensLabel.text = @"beginnt";
_preteritumLabel.text = @"begann";
_perfektumLabel.text = @"hat begonnen";
}
}