Question

Okay, so i need som help. My code do almost what i want it to do, but there is a small problem i think. I have a table view that gets its information from a .plist file and my code lays it out in the table view. Now, what i want is that when i press a table view cell, i want four different labels to change to something i have spesificed in the code. I got this to work, but only in the first section(I have sections A-B). When i pressed the first table view cell in the second section, i got the same values for my labels as the first cell in the first sections. Any ideas how to get this working?

Her is my view controller.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

Here is my 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;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

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

I have several more else if's to put in the code, but it will be a long list. If you could show me how to put the first "if" in the B-section and the "else if" in the E-section. And maybe how to get something else in another section, it would be great!

Thanks for any help i get!

Was it helpful?

Solution

As I earlier mentioned in comment here's the code snipeet showing how to accomplish the check for section. By default it works for section 0. Not for other sections.

here's the code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 if (indexPath.section == 0) {

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

}

else if (indexPath.section == 1){

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

}

}

Look into this and let us know if you still have any problem.

Thnks :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top