오케이, 도움이 필요합니다. 내 코드는 내가 원하는 것을 거의 수행하지만, 생각에는 작은 문제가있다. . plist 파일에서 정보를 가져 오는 테이블보기가 있고 내 코드가 테이블보기에 배치합니다. 자, 내가 원하는 건 내가 테이블 뷰 셀을 누를 때, 나는 네 개의 다른 레이블이 코드에서 구체화 된 것으로 바뀌길 바란다. 나는 이것을 작동 시키지만, 첫 번째 섹션 (A-B 섹션 만)에만 있습니다. 두 번째 섹션에서 첫 번째 테이블 뷰 셀을 누를 때 첫 번째 섹션의 첫 번째 셀과 동일한 레이블 값을 얻었습니다. 이 아이디어를 얻으려면 어떤 아이디어가 필요합니까? 여기 섹션의 특수 테이블보기 셀을 눌렀을 때 여러 레이블의 텍스트를 변경하는 방법은 무엇입니까?
#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입니다 :
그녀는 내보기 controller.h 인 코드를 넣어 만약 내가 몇 가지 더 다른 사람이
#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";
}
}
하지만이 될 것입니다 긴 목록. B 섹션에 첫 번째 "if"를 넣고 E 섹션에 "else if"를 넣는 방법을 보여줄 수 있다면. 그리고 아마 다른 섹션에서 다른 것을 얻는 방법, 그것은 좋을 것입니다!
도움을 주셔서 감사합니다.
tableview에서 섹션을 차별화하기 위해 indexPath.section을 사용하십시오. – Balu
예, indexPath.row와 같이 섹션을 검사하지 않았습니다. indexPath.row를 indexPath.section과 함께 사용하여 각 섹션의 행을 구별합니다. –
어떻게 추가하나요? 그냥 작성합니까 : "else if (indexPath.section == 1, indexPath.row == 1)? –