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";
}
}
是的,你需要添加一些邏輯到tableView:didSelectRowAtIndexPath:方法,並在那裏設置你的標籤的值。那麼,你不知道該怎麼做? – rdelmar 2013-04-07 16:15:29
我不知道該寫什麼tableview:difSelectRowAtIndexPath:方法。應該是這樣的嗎? if tableViewRow = NSString @「The_Name_Of_One_Of_The_Rows」 – 2013-04-07 16:30:50
不,這是完全錯誤的 - 行沒有名字。您使用傳入該方法的indexPath根據行號作出任何決定。你真的需要閱讀關於UITableView的Apple文檔。 – rdelmar 2013-04-07 16:40:35