3

这里又是 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),当我填写代码时,它对于第一部分非常有效。但是,当我按下下一节中的一个单元格时,它显示的信息与第一节中的第一个单元格相同。为什么?

- (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";
    }

}
4

1 回答 1

1

如果您在其中设置文本,则在调用tableview:didSelectRowAtIndexPath:时将被覆盖tableview:cellForRowAtIndexPath:

在内部tableview:didSelectRowAtIndexPath:,您需要访问驱动表的数据并从源头更改它。

NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];

nameSection[indexPath.row] = @"New Value";
于 2013-04-08T01:35:42.373 回答