今天使用tableView是遇到一个问题,如图
这个tableView中使用了footerview和headerView代理方法中我是这么写的
//返回组头view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return nil;
}
TCPersonalAboutHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutHeaderCell];
if (!headerView) {
headerView = [[TCPersonalAboutHeaderView alloc] initWithReuseIdentifier:personalAboutHeaderCell];
}
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return 0;
}
return 50;
}
//返回组头尾view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return nil;
}
TCPersonalAboutFooterView *footerview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutFooterCell];
if (!footerview) {
footerview = [[TCPersonalAboutFooterView alloc] initWithReuseIdentifier:personalAboutFooterCell];
}
return footerview;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return 0;
}
return 60;
}
出现上边的原因是因为我返回了nil,高度最好也不要设为0;
改正后的代码
//返回组头view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN)];
}
TCPersonalAboutHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutHeaderCell];
if (!headerView) {
headerView = [[TCPersonalAboutHeaderView alloc] initWithReuseIdentifier:personalAboutHeaderCell];
}
headerView.dataModel = aboutModel;
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return CGFLOAT_MIN;
}
return 50;
}
//返回组头尾view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 0) {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN)];
}
TCPersonalAboutFooterView *footerview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutFooterCell];
if (!footerview) {
footerview = [[TCPersonalAboutFooterView alloc] initWithReuseIdentifier:personalAboutFooterCell];
}
return footerview;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
if (aboutModel.sublist.count <= 2) {
if (aboutModel.sublist.count <= 0) {
return CGFLOAT_MIN;
}
return 10;
}
return 60;
}
感谢坐我旁边的的小二货。。。。