gpt4 book ai didi

ios - reloadRowsAtIndexPaths 搞乱了 animateWithDuration

转载 作者:行者123 更新时间:2023-11-29 01:28:56 27 4
gpt4 key购买 nike

我试图同时绑定(bind)两个 Action 。其中之一动态更改 heighForRow,第二个动态地拉伸(stretch) View.frame。两者都通过相同的点击被解雇。问题是当我尝试添加 reloadRowsAtIndexPath触摸后,当行高发生变化时,不幸的是行标题和颜色“乱七八糟”。例如,在触摸第二行的位置后,第一行显示,第三行显示第一行,每次单击依此类推但是更改 rowHeight 有效!并且日志显示正确的行被触摸 - (所以当我触摸第一个时,第一个用 NSLog 记录)。有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){

cell = [[PrototypeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setIndentationWidth:0.0];

PrototypeCell *prototypeCell =(PrototypeCell *)cell;
prototypeCell.View = [[UIView alloc]init];
prototypeCell.View.frame = CGRectMake(0,0, 15, 120);
prototypeCell.View.alpha = .5f;
[cell addSubview:prototypeCell.View];
DetailView *detailView = [[DetailView alloc] initWithFrame:CGRectMake(0, 120, 320, 120)];
[cell addSubview:detailView];
prototypeCell.detailView = detailView;

if (indexPath.row == 0){

prototypeCell.View.backgroundColor = [UIColor redColor];
cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

}
if (indexPath.row == 1){

prototypeCell.View.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];


}
if (indexPath.row == 2){
prototypeCell.View.backgroundColor = [UIColor blueColor];
cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

}}

cell.textLabel.font = [UIFont boldSystemFontOfSize:21];
cell.detailTextLabel.font = [UIFont italicSystemFontOfSize:13];
cell.accessoryType = UITableViewCellAccessoryNone;

PrototypeCell *prototypeCell =(PrototypeCell *)cell;
prototypeCell.detailView.hidden = !expanded[indexPath.row];

return cell;

}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

int selectedRow = indexPath.row;
NSLog(@"touch on row %d ", selectedRow);

PrototypeCell *prototypeCell =(PrototypeCell *)[tableView cellForRowAtIndexPath:indexPath];

[self.tableView beginUpdates];
expanded[indexPath.row] = !expanded[indexPath.row];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

if (prototypeCell.stretched == NO){
[UIView animateWithDuration:1 animations:^{

prototypeCell.View.frame = CGRectMake(0, 0, 320, 120);
}
completion:nil];
prototypeCell.stretched = YES;

}

else {

[UIView animateWithDuration:1 animations:^{

prototypeCell.View.frame = CGRectMake(0, 0, 15, 120);
}
completion: nil];
prototypeCell.stretched = NO;

}
}

最佳答案

问题在于您如何使用 dequeueReusableCellWithIdentifier,因为当单元格被重用时,您的以下代码将不会执行。

if (indexPath.row == 0){

prototypeCell.View.backgroundColor = [UIColor redColor];
cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

}
if (indexPath.row == 1){

prototypeCell.View.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];


}
if (indexPath.row == 2){
prototypeCell.View.backgroundColor = [UIColor blueColor];
cell.textLabel.text = [self.thingList objectAtIndex:indexPath.row];

}

此代码仅在您的单元格为 nil 时执行,如果您不想重用单元格,则必须将此代码移出 nil block ,否则如果您的单元格有限并且每个单元格都不同,请不要重用。

完成动画后,也将这两行移到最后

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

关于ios - reloadRowsAtIndexPaths 搞乱了 animateWithDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com