简体   繁体   中英

Can't Reorder a UITableViewCell into _some_ empty sections of a UITableView

If I have a UITableView in edit mode, w/ reordering turned on, it seems I can't move some (but not all) cells into some (but not all) empty sections. For example, if I have this layout :

Section 1
  apple
  banana
Section 2
  doberman
Section 3
Section 4

Then I can move 'doberman' into any slot in section 1 (except after 'banana'), but I can't move it into section 3 or 4 at all.

On the other hand, I can move 'apple' & 'banana' into section section 2 (except after 'doberman'), and I CAN move it into section 3, but NOT into section 4.

What gives? this seems like buggy behavior. How do people work around it? Is apple aware of this bug?

Yup, it's an apple bug. I talked to apple, used one of my "support tickets" for them to tell me that it's their bad.

I did also hack around it, by moving from the obvious many section table model to a model that only has a single section, and models "section headers" as styled table view cells.

Stupid apple...

I have used another hack: create a dummy last section with an empty cell (invisible backgroundView) in the edit mode. With this, i can keep the section/row organization, and the reordering works wonderfully, even with the "last" section.

Another way, is to have a section footer with, at least, the same height as your cells. In this case, the cell can go past the last cell of the last section.

In this blog tells you the easiest way to do this:

http://davemeehan.com/technology/moving-uitableviewcell-between-sections-in-grouped-uitableview

No matter if the section is empty. Simply return some text in titleForFooterInSection: and you'll be able solve the problem. It even works by returning @"".

Sirdek is correct. Just copy the following code into your UITableViewDelegate/UITableViewDataSource.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSInteger lastSection = [self numberOfSectionsInTableView:tableView] - 1;
    NSInteger lastRow = [self tableView:tableView numberOfRowsInSection:lastSection] - 1;
    CGFloat height = [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:lastSection]];

    return (section == lastSection ? [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)] autorelease] : nil);
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSInteger lastSection = [self numberOfSectionsInTableView:tableView] - 1;
    NSInteger lastRow = [self tableView:tableView numberOfRowsInSection:lastSection] - 1;
    CGFloat height = [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:lastSection]];

    return (section == lastSection ? height : 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM