开发者

Gap appears after removing section from Grouped UITableView

I have 3 sections in my table, each section containing at most 1 row.

However, the center row, depending on a variable, may or may not be displayed in the tableview. My code implementation for this as follows:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *currentUserId = [[UserStockInfo sharedUserStockInfo]getUserId];
    switch (section) {
        case 0: //Follow button
            return 1;
            break;

        case 1://Center cell
            if (self.user.exists) {
                return 1;
            }
            else {
                return 0;
            }
            break;

        case 2://question
            return 1;

        default:
            return 0;
            break;
        }

}

The issue I am facing is that for cases whereby the c开发者_运维技巧enter cell is not supposed to be displayed, a gap still appears between the first and third sections (as seen below). How can I get rid of the excess gap after I remove the center cell?

With center cell/section:

Gap appears after removing section from Grouped UITableView

Without center cell/section (gap appears):

Gap appears after removing section from Grouped UITableView


You should set appropriate heights of footer and header of your UITableView.

Implement following methods in your delegate:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

For example, for second section you could return 0:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
     if (section == 1) return 0;
     else return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
     if (section == 1) return 0;
     else return 10;
}

Hope, this will help you


I think you should implement (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.user.exists) {
        return 3;
    }
    return 2;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜