gpt4 book ai didi

ios - 自定义移动tableview单元格: How to always keep first cell at top of UITableView when "moveRowAtIndexPath"

转载 作者:行者123 更新时间:2023-12-01 18:49:47 25 4
gpt4 key购买 nike

当我移动其他单元格时,我需要保持我的第一个单元格始终位于 tableview 的顶部。我花了很多时间和多种方式按钮,我还没有弄清楚如何解决这个问题。
这是我的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...do something to custom first cell design from xib file

//...do some thing to custom normal cells(cells at below of first cell)

[firstcell setEditing:NO animated:YES];
firstcell.userInteractionEnabled=NO;

if (indexPath.row==0)
{
return firstcell;
}
else
{
return cell;
}
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) // Don't move the first row
return NO;
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// i just change datasource for tableview at here
}

当我移动单元格(普通单元格)时,会有我的表格 View 。

我想保持第一个单元格(蓝色单元格)始终位于顶部,而不是与其他单元格交互。

最佳答案

您需要再实现一种委托(delegate)方法:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
if (proposedDestinationIndexPath.row == 0) {
// Don't allow a row to be moved to the first row position
return [NSIndexPath indexPathForRow:1 inSection:0];
} else {
return proposedDestinationIndexPath;
}
}

此代码假定您的表格 View 中只有一个部分。

这个方法的目的是告诉 TableView ,如果被移动的行的建议目的地不合适,则应该使用返回的值。如此处所写,任何将行移动到顶部的尝试都将导致它被移动到顶行的下方。

关于ios - 自定义移动tableview单元格: How to always keep first cell at top of UITableView when "moveRowAtIndexPath",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158595/

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