热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

TableView和Detail视图单元格显示-TableViewandDetailviewcelldisplay

ImlearninghowtomakeacontactusingTableViewandnavigationcontroller.Ihavedonethefirst

I'm learning how to make a contact using TableView and navigation controller. I have done the first view page with some cells, but I have some problem with showing the cells on the detail view page of each cell from first view. when tap the Group A, Group B, Group C on first view page, it goes to the detail view of each cell, it should show cells named A1 A2 A3, B1 B2 B3 or C1 C2 C3 according to my code on detail view page. but it display nothing. Hope someone can give me some advises, thank you so much!

我正在学习如何使用TableView和导航控制器进行联系。我已经完成了第一个包含一些单元格的视图页面,但是我在第一个视图中显示每个单元格的详细视图页面上的单元格时遇到了一些问题。当在第一个视图页面上点击A组,B组,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

first view

first view

detail view but nothing displayed

细节视图,但没有显示

detail view but nothing displayed

1 个解决方案

#1


0  

Have you set the delegate and datasource of tableview?

您是否设置了tableview的委托和数据源?

If Yes, check if the datasource methods (numberOfRowsInSection:) are called after you do

如果是,请检查是否在执行后调用了数据源方法(numberOfRowsInSection :)

groupA = [NSArray arrayWithObjects:@"A1",@"A2",@"A3",nil]; in viewDidLoad method.

groupA = [NSArray arrayWithObjects:@“A1”,@“A2”,@“A3”,nil];在viewDidLoad方法中。


推荐阅读
author-avatar
iar2984165
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有