【问题标题】:How to change the text in several labels when a special table view cell from a section is pressed?按下部分中的特殊表格视图单元格时,如何更改多个标签中的文本?
【发布时间】:2013-04-08 13:58:25
【问题描述】:

好的,所以我需要一些帮助。我的代码几乎完成了我想要它做的事情,但我认为有一个小问题。我有一个表格视图,它从 .plist 文件中获取其信息,我的代码将其放在表格视图中。现在,我想要的是,当我按下表格视图单元格时,我想要四个不同的标签更改为我在代码中指定的内容。我得到了这个工作,但只在第一部分(我有 A-B 部分)。当我按下第二部分中的第一个表格视图单元格时,我的标签值与第一部分中的第一个单元格相同。任何想法如何让它工作?

她是我的视图控制器.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;


}

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

我还有其他几个如果要放入代码中,但这将是一个很长的列表。如果你能告诉我如何将第一个“if”放在 B 部分,将“else if”放在 E 部分。也许如何在另一个部分中获得其他东西,那就太好了!

感谢您的帮助!

【问题讨论】:

  • 使用 indexPath.section 区分 tableview 中的部分。
  • 是的,您没有像 indexPath.row 那样对该部分进行任何检查。使用 indexPath.row 和 indexPath.section 来区分每个部分的行。
  • 我如何添加它?我只是写:“else if (indexPath.section == 1, indexPath.row == 1)?

标签: iphone objective-c xcode tableview tableviewcell


【解决方案1】:

正如我之前在评论中提到的,这里的代码片段显示了如何完成对部分的检查。 默认情况下,它适用于第 0 部分。不适用于其他部分。

代码如下:

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

}

}

调查一下,如果您还有任何问题,请告诉我们。

谢谢:)

【讨论】:

    猜你喜欢
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多