简体   繁体   English

Tableview没有显示?

[英]Tableview is not showing?

I am making tableview with different sections programmatically. 我正在以编程方式制作具有不同部分的tableview。 I am using this code Please help me where i am wrong. 我正在使用此代码,如果我错了,请帮助我。 I have to show different sections with different cells ,sections of years and cells of movies. 我必须显示具有不同单元格的不同部分,年份的部分和电影的单元格。

in .h file 在.h文件中

{
 NSDictionary *movieTitles;
    NSArray *years;
}
@property (nonatomic, retain) NSDictionary *movieTitles;
@property (nonatomic, retain) NSArray *years;

in .m file 在.m文件中

@synthesize movieTitles;
@synthesize years;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"about" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];

    movieTitles = dic;


    aboutTable = [[UITableView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, self.view.frame.size.height) style:UITableViewStyleGrouped];
    aboutTable.delegate = self;
    aboutTable.dataSource = self;

    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 50, 20)];
    [backButton addTarget:self action:@selector(backdb) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [self.view addSubview:backButton];

    [self.view addSubview:aboutTable];


}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [years count];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *details = [years objectAtIndex:section];
    NSArray *titleSection = [movieTitles objectForKey:details];

    return [titleSection count];

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellidentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSString *details = [years objectAtIndex:[indexPath section]];
    NSArray *titleSection = [movieTitles objectForKey:details];
    cell.textLabel.text = [titleSection objectAtIndex:[indexPath row]];


    return cell;

}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *details = [years objectAtIndex:section];
    return details;

}
  1. You are returning the [years count] for numberOfSectionsInTableView. 您将返回numberOfSectionsInTableView的[年计数]。 Where you initialise the years? 您在哪里初始化年份?
  2. And also in viewDidLoad, instead of movieTitles = dic, use the below code:- 同样在viewDidLoad中,而不是movieTitles = dic,请使用以下代码:

    movieTitles = [dic copy]; movieTitles = [dic复制];

where is your "year" value. 您的“年”值在哪里? It count become Zero. 计数变为零。 and you have to alloc your value.so first of all add object to array. 并且您必须分配您的value.so首先将对象添加到数组。

eg years =[[NSArray alloc]initWithObjects:@"2001",@"2002",@"2003", nil]; 例如years =[[NSArray alloc]initWithObjects:@"2001",@"2002",@"2003", nil];

Table view don't show beacuse "years is nil" when called 调用时表视图未显示“ years is nil”

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
   return [years count];
}

as the number of section return 0, so the tableveiw could not add. 由于节的编号返回0,因此tableveiw无法添加。

resolve this issue. 解决此问题。

add this line in viewDidLoad method viewDidLoad方法中添加此行

years  = @[@"2015",@"2015"];
- (void)viewDidLoad {

NSArray *arrYear;
arrYear=[[NSArray alloc]initWithObjects:@"1991",
@"1992",
@"1993",
@"1994",nil]

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
          NSString *cellIdetifier = [NSString stringWithFormat:@"Cell_%@",indexPath];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier ];

    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];

      cell.textLabel.text = [arrYear objectAtIndex:[indexPath row]];
    }

    return cell;

}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
   return years.count;
}
arrYear=[[NSArray alloc]initWithObjects:@"1991",
@"abc",
@"xyz",
@"123",nil]

You have to initialise Your Array values in didload 您必须在didload中初始化数组值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM