Zigii Wong
Zigii Wong

Reputation: 7826

How to make a cell overlap?

I want to make cells overlapping.

enter image description here

What I did is Adjust the tableview's contentSize:

int count = [self.tableView numberOfRowsInSection:0];
self.tableView.contentSize = CGSizeMake(self.view.frame.size.width ,(100 * count - 30 * (count - 1)));

And set the cellForRowAtIndexPath :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
      cell.contentView.backgroundColor = [UIColor orangeColor];
    }
    if (indexPath.row != 0) {
      UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]];
      [currentCell setFrame:CGRectMake(0, - 30, self.view.frame.size.width, 100)];
    }
    return cell;
}

But when I test, this is not working.

Any help? Thanks.

Upvotes: 1

Views: 566

Answers (1)

mttcrsp
mttcrsp

Reputation: 1661

What is happing with your code is that the values that you are manually setting inside tableView:cellForRowAtIndexPath: are getting overwritten by the UITableViewController layout methods.

You are not supposed to edit the frame of a table view cell directly since this is an attribute that is set by UITableViewController internally. You should only interact with this by using methods of the UITableViewDelegate protocol. (tableView:heightForRowAtIndexPath:, ...)

IMHO the best way (cleanest one) to implement what you are describing is to subclass UICollectionView instead of UITableView and define a custom layout.

However if you wanna go the hacky way you can achieve an apparent row overlapping in many different ways.

Just as an example, you could create two different row types, one with height 30 and one with height 70, where the first one represents the overlapping part of the row and the second one represents the not-overlapping part of the row. Then use the first type for even rows and the second one for odd rows.

Hope this helps.

p.s. i'm really sorry if my english is not the best

Upvotes: 1

Related Questions