在自定义UITableViewCell中更改自定义UIButton的位置

我需要dynamic地改变UIButton的位置。 我在cellForRowAtIndexPath:做这个cellForRowAtIndexPath: 我改变该方法中的button的框架。 最初显示表时,不会显示更改。 但是当我滚过单元格并回到它时,就会显示出来。 同样,当我第一次滚动到最初不可见的单元格时,没有任何变化。 当我第二次滚动时,会发生变化。

我已经尝试setNeedsDisplay:在自定义button和表视图。 我甚至在willDisplayCell:forRowAtIndexPath:做了这个。 我如何解决这个问题?

编辑:我忘了提及,我从一个nib文件加载UITableViewCell。 这里是调整框架的代码。

 label1.text = //text from some source label1.text = [label1.text stringByAppendingString:@" |"]; [label1 sizeToFit]; CGRect frameAfterResize=label1.frame; float x=frameAfterResize.origin.x+frameAfterResize.size.width; button.frame=CGRectMake(x+2, button.frame.origin.y, button.frame.size.width,button.frame.size.height); 

制作你自己的UITableViewCell子类。 在init方法中创build并添加button到单元格的contentView中,或者通过访问器属性来创build并添加它。 覆盖layoutSubviews和位置button按需要。

像这样的东西:

 @implementation MyCustomCell - (void) init { self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil]; if ( self != nil ) { _myButton = [[UIButton buttonWithType: UIButtonTypeRoundedRect] retain]; [self.contentView addSubview: _myButton]; } return self; } - (void) layoutSubviews { [super layoutSubviews]; // dynamic layout logic: if ( ... ) { _myButton.frame = CGRectMake( 10, 10, 100, 30 ); } else { _myButton.frame = CGRectMake( 20, 10, 50, 30 ); } } 

或者,您可以尝试在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

我有同样的问题,我从一个笔尖加载细胞,但没有上述解决scheme完全为我工作。 他们解决了less数情况下的问题,但不是全部。

这是适用于所有场景的解决scheme。

 @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 

你有没有尝试通过使用更改单元格的框架后重新加载表

 [self.tableView reloadData] 

我认为这应该解决你的问题。

  1. 制作你自己的UITableViewCell子类
  2. 连接所有的sockets(button以及)从你的xib这个类
  3. 例如,在这个UITableViewCell子类中创build自定义方法 – 参见下面的代码,不能在那里格式化它。
  4. 在cellForRowAtIndexPath中:使用此方法更改框架并重绘单元格

    [cell setNewFrame:….];

这对我来说可以

  -(void)setNewFrame:(CGRect)newFrame { myButton.frame=newFrame; [self setNeedsDisplay]; } 

如果你想调整button的文字

 -(void)setNewCaption:(NSString)newCaption { label1.text = newCaption label1.text = [label1.text stringByAppendingString:@" |"]; // calculate new frame myButton.frame=//new frame [self setNeedsDiplay]; } 

用你的UITableViewCell对象调用setNeedsDisplay

 [cell setNeedsDisplay]; 

cellForRowAtIndexPath:

如果最初没有按要求显示,但在滚动后显示,那么我的猜测是,只有当一个单元格出列时,而不是从笔尖初始加载时,才会改变标签的位置。

你还没有给你的cellForRowAtIndexPath方法的这一部分,所以我不能给这个任何更具体的build议,但是上面的代码是不是正在执行,或者label1是零,当一个新的单元格(即当出列方法返回零,你从笔尖加载)。

绘制UITableViews时,表格单元格被重用,所以只有一个由传递给dequeueReusableCellWithIdentifier的string标识的UITableViewCell实例。

如果更新cellForRowAtIndexPath:方法中的单元格,它将不会产生您所期望的效果 – 每次您将引用同一个单元格(因此每个单元格将具有相同位置的button)。 因此,您所看到的滚动的问题使button更改位置。

解决问题的最简单的方法是改变代码,通过每次在cellForRowAtIndexPath中创build一个新的单元来停止重新使用单元格。 像这样更新代码:

 // comment out this line // cell = [tableView dequeueReusableCellWithIdentifier:identifier]; NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName]; cell = [nib objectAtIndex:0]; 

请注意,尽pipe出于效率原因,重复使用单元是首选(以节省内存并减less内存分配)。

你理想的解决scheme是创build一个UITableViewCell的自定义子类,并在layoutSubviews方法中更新UIButton的位置。