自定义UITableView重新排序

Pud*_*all 7 iphone cocoa-touch uitableview uikit

我想知道是否有人有一个好教程的链接或可以指出我正确的方向重新创建像UICableView像Epic Win App中的'拖动重新排序'单元格.基本的想法是点击并按住列表项,然后拖动到您想要项目的位置.任何帮助将不胜感激.

Dan*_*ark 13

使用内置方法的最简单方法如下:

首先,设置表格单元格以显示重新排序控件.最简单的例子(使用ARC):

这是假设NSMutableArray *things已在本课程的其他地方创建

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"do not leak me"];
    if (!cell) {
        cell = [[UITableViewCell alloc] init];
        cell.showsReorderControl = YES;
    }
    cell.textLabel.text = [things objectAtIndex:indexPath.row];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

实现你的两个UITableViewDelegate方法,如下所示:

此方法告诉tableView允许重新排序

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

这是tableView重新排序实际发生的地方

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    id thing = [things objectAtIndex:sourceIndexPath.row];
    [things removeObjectAtIndex:sourceIndexPath.row];
    [things insertObject:thing atIndex:destinationIndexPath.row];

}
Run Code Online (Sandbox Code Playgroud)

然后以某种方式,某处,将编辑设置为true.这是您如何做到这一点的一个例子.tableView必须处于编辑模式才能重新排序.

- (IBAction)doSomething:(id)sender {
    self.table.editing = YES;
}
Run Code Online (Sandbox Code Playgroud)

希望这是一个具体的例子.


Bra*_*rad 7

这是非常直接的 - 这很明显是为什么没有明确的教程.

只需正常创建UITableView,但将showsReorderControl每个单元格设置为TRUE.当您进入编辑模式时(通常按下"编辑"按钮并将UITableView的"编辑"值设置为TRUE) - 重新排序栏将出现在单元格中.

注意:

如果您的数据源实现tableView:canMoveRowAtIndexPath:- 并且它返回"NO" - 则不会出现重新排序栏.

您还需要 –tableView:moveRowAtIndexPath:toIndexPath:在数据源委托中实现.

  • 我希望能够做的一件事是取代按钮并使整个单元格成为按钮.所以你可以拖动细胞.你知道怎么做的吗?或者是如何做到这一点的垫脚石? (5认同)
  • 我同意Thomson的说法,这远非一个简单的问题,而且比使用几个tableView委托方法和编写子类要深入得多.如果您声称容易,请提供简单的证明. (2认同)