0

当重新选择(触摸而不抬起手指)已选择的单元格时,存在有关UITableViewCell的textLabel属性的问题。UITableViewCell在选择/突出显示同一单元格后,textLabel高度会重置

我有一个自定义的contentView被添加到textLabel下面的背景中,该textLabel具有动态高度(包括单元格)和textLabel的背景以及始终相同的高度(50.0f-ish)。

我正在使用beginUpdates和endUpdates方法为选择和帧更改设置动画,以防止textLabel在更新发生后立即在单元中居中;大致保持在背景的相同范围内。选择不同的单元格时,没有任何问题,并且textLabel会保留在应该显示的顶部。

现在的主要问题是当用户再次触摸手指(而不是抬起它)在选定的单元格上时,textLabel会根据单元格的高度更改重新调整。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     DetailView *detailView = // Create detail view 

     // Add UIImageView from content to cell -------------------- 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

     // Change textLabel colors, fontSize, and shadow properties 
     // Create CustomContentView *ccView = nil; 


     // ------ 
     // Remove/Hide any existing CustomContentViews 
     // --- Code here 
     // ------- 

     switch (hasImageInCustomView) { 
     case YES: 
      image = _imageInCustomView; 
      image = NO; 

      if (selectedIndexPath.row != indexPath.row) { 
       [tableView beginUpdates]; 
       [tableView endUpdates]; 
      } 

      CGRect frame = cell.textLabel.frame; 
        frame.size.height = 50.0f; 
        cell.textLabel.frame = frame; 


      return; 
     break; 
     default: 
      image = [detailView getImage]; 
     break; 
     } 

     // ---- 
     // Calculate height for image and add that to heightForExpandedRow property 
     // -- Code here... 
     // ---- 

     [tableView beginUpdates]; 
     [tableView endUpdates]; 

     // Add subview with UIImageView 
     customContentView = [[CustomContentView alloc] initWithFrame:(CGRect){0, 0, kWidth, dynamicExpandedRow}]; 
     contentView.clipsToBounds = YES; 
     [cell.contentView addSubview:customContentView]; 
     [cell.contentView sendSubviewToBack:customContentView]; 
     [customContentView setContentImage:image]; 

     CGRect frame = cell.textLabel.frame; 
     frame.size.height = 50.0f; 
     cell.textLabel.frame = frame; 

} 

我使用的是iOS 3.0,所以我不认为didHighlightRowAtIndexPath:将工作,如果有任何委托方法是重置为textLabel框架beginUpdates/endUpdates一直呼吁后,我想知道。

我在登录框的变化和它出现像这样:

之前beginUpdates/endUpdates被称为

<UILabel: 0x4e55500; frame = (10 0; 250 199); text = 'Some Text Goes Here...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4e55570>> 

beginUpdates/endUpdates一直呼吁和手动帧后变更

<UILabel: 0x4e55500; frame = (10 0; 250 50); text = 'Some Text Goes Here...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4e55570>> 

高度显示199前这两种方法被调用,之后是50。 但是,无论强制更改帧,只要选定的单元格被重新选择,textLabel就会在单元格中垂直重新垂直重新打开。

如果有人可以帮助,我将不胜感激。

链接到图片的例子 - >http://i50.tinypic.com/2r6o5k5.jpg

+0

我倾向于继承UITableViewCell的子类并使用基于相关问题的layoutSubviews。 – Bill 2013-03-20 03:58:47

+0

不幸的是,继承UITableViewCell并使用layoutSubviews无法正常工作。当用户重新选择一个选定的单元格时,问题仍然存在,并且将textLabel的尺寸重置为适合单元格的新展开高度,同时垂直居中文本。 – Bill 2013-03-20 04:21:04

回答

0

你正在改变的UILabel高度时选定单元格的权利,那么你改变的UILabel高度前加一个条件。

即 最初selectedIndexRow将为-1。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
//Check if we are selecting the same cell 
     if(selectedIndexRow != indexPath.row){ 
      if(selectedIndexRow != -1){ 

     //deselect the previous cell 
     } 

      selectedIndexRow = indexPath.row; 

     //preform your action 

     } 
    } 

当你正在检查的selectedIndex添加上述条件太..你并不需要太大改变你的代码..

+0

嗯,我试图保持textlabel固定在顶部,当单元格高度展开(没有太多的视觉变化,除了字体大小和颜色)。 – Bill 2013-03-20 04:03:59

+0

@Bill你可以添加快照。所以它会帮助我们 – Anand 2013-03-20 04:37:48

+0

好吧,给我一个:) – Bill 2013-03-20 04:56:20

0

试试这个:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 50; 
} 

你必须增加单元格高度,因为你使用默认的标签不是自定义的

+0

我已经这样做了,但谢谢你的洞察力! – Bill 2013-03-20 05:02:18

+0

ok :)欢迎.. – Abha 2013-03-20 05:14:18

相关问题