iOS:未显示 UITableView 重新排序控件



我正在设置所有必需的属性,以便在我的iOS应用程序中实现UITableView的重新排序控件,但是没有显示重新排序控件。代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DragAndDrop";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[[NSString alloc]initWithFormat:@"Row %d", indexPath.row];
    cell.showsReorderControl=YES;
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setEditing:YES animated:YES];
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
         [tableView reloadData];
}

我已经实现了所有必需的方法。为什么它没有出现?

为此

,您需要从一开始就设置UITableView的"编辑"属性。

希望对您有所帮助。

干杯!

我忘了将情节提要中的委托链接到表视图。现在一切都很好!!

试试这个。 。 。这将在编辑模式下的简单表格视图的情况下处理单元格的排列和更新

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
  return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
     [tableData insertObject: [tableData objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
     [tableData removeObjectAtIndex:(sourceIndexPath.row + 1)];
}

最新更新