【发布时间】:2013-03-29 15:30:22
【问题描述】:
这里又是 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";
}
}
【问题讨论】:
-
是的,您需要在 tableView:didSelectRowAtIndexPath: 方法中添加一些逻辑,并在那里设置标签的值。那么,你不知道具体怎么做?
-
不知道在tableview:difSelectRowAtIndexPath:方法里写什么。应该是这样的吗?如果 tableViewRow = NSString@"The_Name_Of_One_Of_The_Rows"
-
不,这是完全错误的——行没有名字。您使用传入该方法的 indexPath 来根据行号做出任何决定。你真的需要阅读 Apple 关于 UITableView 的文档。
-
看看我想做什么,在描述中,有什么想法吗?
-
你使用 indexPath.row 就像我上面说的那样。如果 indexPath.row == 0 那么一些 label.text = @"whatever",否则如果 indexPath.row = 1 做别的事情。另外,当你已经有一个字符串时,不要使用 stringWithFormat 。应该是这样的:_norskLabel.text = @"å komme";
标签: ios xcode label tableview tableviewcell