Reputation: 185
I'm trying to have my tableView within my view controller return multiple sections. However whenever I do this and end up putting text in the section header, I get a black screen but no errors. However, changing down to one section and no header text entered in the storyboard or in code, will show the tableview correctly. Is there something I'm doing wrong?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return @"Header1";
}
else if (section == 1)
{
return @"Header2";
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
long num = indexPath.row;
UITableViewCell *cell;
switch (num)
{
case 0:
cell = self.firstCell;
break;
case 1:
cell = self.secondCell;
break;
case 2:
cell = self.thirdCell;
break;
case 3:
cell = self.fourthCell;
break;
}
return cell;
}
Upvotes: 0
Views: 129
Reputation: 17535
Your cell is not initilized so first intilize it. And for more info see this code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
switch (indexPath.row)
{
case 0:
cell = self.firstCell;
break;
case 1:
cell = self.secondCell;
break;
case 2:
cell = self.thirdCell;
break;
case 3:
cell = self.fourthCell;
break;
}
return cell;
}
else
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
switch (indexPath.row)
{
case 0:
cell = self.firstCell;
break;
case 1:
cell = self.secondCell;
break;
case 2:
cell = self.thirdCell;
break;
case 3:
cell = self.fourthCell;
break;
}
return cell;
}
}
Upvotes: 2