gpt4 book ai didi

ios - 当有多个部分时, TableView 是黑色的

转载 作者:行者123 更新时间:2023-11-28 22:18:45 25 4
gpt4 key购买 nike

我试图让我的 View Controller 中的 tableView 返回多个部分。但是,每当我这样做并最终将文本放在节标题中时,我都会出现黑屏但没有错误。但是,向下更改为一个部分并且没有在 Storyboard或代码中输入标题文本,将正确显示表格 View 。我做错了什么吗?

- (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;
}

最佳答案

您的单元格未初始化,请先将其初始化。有关详细信息,请参阅此代码。

    -(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;
}
}

关于ios - 当有多个部分时, TableView 是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20964740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com