以下是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";
}
}
好的,所以我的表视图分为(A-Z)部分,当我填写代码时,它完全适用于第一部分。但是,当我按下下一部分中的一个单元格时,它显示的信息与第一部分中的第一个单元格相同。为什么?
- (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";
}
}
答案 0 :(得分:1)
如果您在tableview:didSelectRowAtIndexPath:
中设置文字,则会在调用tableview:cellForRowAtIndexPath:
时覆盖该文字
在tableview:didSelectRowAtIndexPath:
内,您需要访问驱动表格的数据并在其来源进行更改。
NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];
nameSection[indexPath.row] = @"New Value";