happycoder
happycoder

Reputation: 977

Add a UIView near to row's button?

I have tableview with sections and rows. When user clicks on button inside a row/section it will display UIView near to that particular button.

- (IBAction) openRoundedPopover:(id)sender {

btn = (UIButton*)  sender;

//Identify if user clicked header section or cell row
if (btn.titleLabel.tag == 8)
    isHeaderViewButtonClicked = YES; // headerview mean when user clicks on section
else
    isHeaderViewButtonClicked = NO;

MainTableViewCell *clickedResourceCell = (MainTableViewCell*)btn.superview.superview;
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];

self.roundedButtonMenu.frame = CGRectMake(43, isHeaderViewButtonClicked ? hitPoint.y+50:clickedResourceCell.frame.origin.y+53, self.roundedButtonMenu.frame.size.width, self.roundedButtonMenu.frame.size.height);

[self.view addSubview:self.roundedButtonMenu];

}

These all working fine. But when the user keep adding rows/sections and clicks on buttons of bottom rows/sections (i.e. scrolls tableview to bottom to view newly added rows/sections) it won't show UIView near to that button. It popups somewhere else or not showing up at all.

Upvotes: 1

Views: 74

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50109

MainTableViewCell *clickedResourceCell = (MainTableViewCell*)btn.superview.superview; 

don't assume a view hierachy you have no control over. (this will 110% break in the near ios future)

So I assume you dont get the cell's frame!

MainTableViewCell *clickedResourceCell = (MainTableViewCell*)btn; 
while(clickedResourceCell.superview && ![clickedResourceCell isKindOfClass:[MainTableViewCell class]]) {
    clickedResourceCell = (MainTableViewCell*)clickedResourceCell.superview;
}

but your problem is that you add the button to elf.view which is not in the scrollview.. the calculation cant really work when scrolling, can it?

add it to the cell:

button.frame = CGRectMake(0,0,100,40); //dummy
 [cell.contentView addSubview:btw];
//take care of cell reusing!

Upvotes: 1

Related Questions