I need to change the position of UIButton dynamically. I do this in cellForRowAtIndexPath:
. I alter the frame of the button in that method. The change is not di
Have you tried reloading the table after changing the cell's frame by using
[self.tableView reloadData]
I think it should solve your problem.
in cellForRowAtIndexPath: use this method to change frame and redraw cell
[cell setNewFrame:....];
it works fine for me
-(void)setNewFrame:(CGRect)newFrame
{
myButton.frame=newFrame;
[self setNeedsDisplay];
}
if you want to adjust button to text
-(void)setNewCaption:(NSString)newCaption
{
label1.text = newCaption
label1.text = [label1.text stringByAppendingString:@" |"];
// calculate new frame
myButton.frame=//new frame
[self setNeedsDiplay];
}
If it doesn't display as required initially, but does after scrolling, then my guess is that you are altering the label position only when a cell is dequeued and not when it is originally loaded from the nib.
You havent given this part of your cellForRowAtIndexPath method so I can't give any more specific advice at this point, but either the code above is not being executed, or label1
is nil when a new cell is made (ie when the dequeue method returns nil and you load from the nib).
When drawing UITableViews, table cells are re-used so there is only one instance of a UITableViewCell identified by the string passed to the dequeueReusableCellWithIdentifier:.
If you update the cell in the cellForRowAtIndexPath: method it will not have the effect you expect - each time you will be referring to the same cell (so each cell will have the button in the same position). Hence the problem you are seeing that scrolling makes the button change position.
The easiest thing to fix your problem is alter the code to stop re-using cells by creating a new cell each time in cellForRowAtIndexPath. Update the code like this:
// comment out this line
// cell = [tableView dequeueReusableCellWithIdentifier:identifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName];
cell = [nib objectAtIndex:0];
Please note though that re-using cells is preferred for efficiency reasons (to save memory and reduce memory allocations).
Your ideal solution is to create a custom sub-class of UITableViewCell and update the postion of the UIButton in the layoutSubviews method.
I had the same issue in which I was loading cell from a nib, but none of the above solutions completely worked for me. They solved the problem for few scenarios but not all of them.
Here is the solution which worked for all scenarios.
@implementation MyChannelCell
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//Calling from here fixed issue for first time cell load
[self alignCellViews];
}
-(void)layoutSubviews
{
[super layoutSubviews];
//Calling from here fixed issue for coming back to the tableview from other ViewConroller
[self alignCellViews];
}
/**
Method to change the layout and positions of sub-views
*/
-(void)alignCellViews
{
//Code to position sub-views
//self.lblSubscribers is an outlet from nib of type UILabel
[self.lblSubscribers setFrame:CGRectMake(140, 26, 36, 26)];
[self setNeedsDisplay];
}
@end
Call setNeedsDisplay
with your UITableViewCell
object.
[cell setNeedsDisplay];
in cellForRowAtIndexPath: