[英]Tableview is not showing?
我正在以編程方式制作具有不同部分的tableview。 我正在使用此代碼,如果我錯了,請幫助我。 我必須顯示具有不同單元格的不同部分,年份的部分和電影的單元格。
在.h文件中
{
NSDictionary *movieTitles;
NSArray *years;
}
@property (nonatomic, retain) NSDictionary *movieTitles;
@property (nonatomic, retain) NSArray *years;
在.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;
}
同樣在viewDidLoad中,而不是movieTitles = dic,請使用以下代碼:
movieTitles = [dic復制];
您的“年”值在哪里? 計數變為零。 並且您必須分配您的value.so首先將對象添加到數組。
例如years =[[NSArray alloc]initWithObjects:@"2001",@"2002",@"2003", nil];
調用時表視圖未顯示“ years is nil”
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [years count];
}
由於節的編號返回0,因此tableveiw無法添加。
解決此問題。
在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]
您必須在didload中初始化數組值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.