【发布时间】:2016-03-21 06:30:50
【问题描述】:
我正在学习如何使用 TableView 和导航控制器建立联系。我已经用一些单元格完成了第一个视图页面,但是我在从第一个视图中显示每个单元格的详细视图页面上的单元格时遇到了一些问题。 当在第一个视图页面上点击 Group A、Group B、Group C 时,它会转到每个单元格的详细视图,它应该根据我在详细视图页面上的代码显示名为 A1 A2 A3、B1 B2 B3 或 C1 C2 C3 的单元格.但它什么也没显示。 希望有人能给我一些建议,非常感谢!
FirstViewController.m
@implementation FirstViewController
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSections(NSInteger)section{
return [self.contactArray count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Celldentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Celldentifier];
if (cell == NULL) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Celldentifier];
}
cell.textLabel.text = [self.contactArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.myTableView = [self.contactArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"To Second View Segue" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"To the second view"]){
NSIndexPath *indexpath = [self.myTableView indexPathForSelectedRow];
self.contactArray = [self.contactArray objectAtIndex:indexpath.row];
SecondViewController *vc = segue.destinationViewController;
vc.memberName = [self.contactArray objectAtIndex:indexpath.row];
vc.title = vc.memberName;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.contactArray = [[NSMutableArray alloc]initWithObjects:@"Group A",@"Group B",@"Group C", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
SecondViewController.m
@implementation SecondViewController{
NSArray *groupA;
NSArray *groupB;
NSArray *groupC;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if([self.memberName isEqualToString:@"groupA"]){
return [groupA count];
}
else if([self.memberName isEqualToString:@"groupB"]){
return [groupB count];
}
else if([self.memberName isEqualToString:@"groupC"]){
return [groupC count];
}
return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Celldentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Celldentifier];
if (cell == NULL) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Celldentifier];
}
if([self.memberName isEqualToString:@"groupA"]){
cell.textLabel.text = [groupA objectAtIndex:indexPath.row];
}
if([self.memberName isEqualToString:@"groupB"]){
cell.textLabel.text = [groupB objectAtIndex:indexPath.row];
}
if([self.memberName isEqualToString:@"groupC"]){
cell.textLabel.text = [groupC objectAtIndex:indexPath.row];
}
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
groupA = [NSArray arrayWithObjects:@"A1",@"A2",@"A3",nil];
groupB = [NSArray arrayWithObjects:@"B1",@"B2",@"B3",nil];
groupC = [NSArray arrayWithObjects:@"C1",@"C2",@"C3",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
第一个视图
详细视图但未显示任何内容
【问题讨论】:
-
你有什么问题?清楚具体地说明您的问题。
-
当您在第一个视图页面上点击 Group A、Group B、Group C 时,它会转到每个单元格的详细视图,它应该显示名为 A1 A2 A3、B1 B2 B3 或 C1 C2 的单元格C3 根据我在详细视图页面上的代码。但它什么也没显示。对不起我的英语不好。
标签: ios objective-c uitableview