我有一个带有标准UITableviewUITableViewCells。在选择一个单元格时,我想用一个用UITextViews而不是detailTextLabelUITextFields而不是attachmentView的自定义单元格替换该单元格。实现此目标的最佳方法是什么?

最佳答案

创建UITableViewCell的子类。将UITextFieldUITextField(适合您的解决方案)添加到contentView中,并使其隐藏。覆盖`-setSelected:animated:'方法:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];
  if ( animated ) {
    // using old-school UIView animation support to fade in/out controls,
    // block-based much easier, but only 4.0 or greater
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    self.textLabel.alpha = (selected) ? 0.0 : 1.0;
    self.detailTextLabel.alpha = (selected) ? 0.0 : 1.0;
    // assumed you added a UITextView 'textView' ivar
    self.textView.alpha = (selected) ? 1.0 : 0.0;
    [UIView commitAnimations];
  }
  else {
    self.textLabel.hidden = selected;
    self.detailTextLabel.hidden = selected;
    self.textView.hidden = !selected;
  }
}

- (void)animationDidStop:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
  self.textLabel.hidden = self.selected;
  self.detailTextLabel.hidden = self.selected;
  self.textView.hidden = !self.selected;
}

关于objective-c - 选择时用自定义单元格替换标准UITableViewCell,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8359344/

10-14 21:28
查看更多