我有一个关于UITableViewCell的textLabel属性重新选择(触摸而不抬起手指)一个已经选择的单元格时的问题。
我有一个自定义的contentView被添加在背景正好低于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/endpdates已被调用。
我已经记录了帧的变化,它出现如下:
在beginUpdates/endpdates被调用之前
<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
当单元格被选中时,您正在更改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时,也添加上述条件。您不需要更改太多代码。
试试这个:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
你必须增加单元格高度因为你使用的是默认标签而不是自定义标签