简体   繁体   English

如何在UITableView中移动没有NSIndexPath的单元格?

[英]How to move cell without NSIndexPath in UITableView?

I am going to move a cell from a section to another in UITableView. 我将在UITableView中将一个单元格从一个部分移动到另一个部分。 The problem is that I don't know the index path of this cell. 问题是我不知道这个单元格的索引路径。 (Or in other words, I have an index path for this cell but the index path is likely out of date now). (换句话说,我有一个这个单元格的索引路径,但索引路径现在可能已过期)。 Instead, I have a reference point to this cell. 相反,我有一个参考点指向这个单元格。 How can I move this cell? 我该如何移动这个细胞?

thanks in advance. 提前致谢。

If you have a reference to the cell's object then you can simply get its indexpath. 如果您有对单元格对象的引用,那么您可以简单地获取其索引路径。

UITableViewCell *cellObject; //provided that you have a reference to it.
NSIndexPath *indexPath = [tableView indexPathForCell:cellObject];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Here is an example of how to move a row based on finding a certain string in the cell to the top of section 1. 下面是如何基于在单元格中找到某个字符串到第1部分顶部来移动行的示例。

@implementation TableController {
    NSInteger selectedRow;
    NSMutableArray *theData;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(70, 0, 0, 0);
    NSMutableArray *colors = [@[@"Black", @"Brown", @"Red", @"Orange", @"Yellow",@"Green", @"Blue"] mutableCopy];
    NSMutableArray *nums = [@[@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight"] mutableCopy];
    theData = [@[colors, nums] mutableCopy];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return theData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [theData[section] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return (section == 0)? @"Colors" : @"Numbers";
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = theData[indexPath.section][indexPath.row];
    return cell;
}



-(IBAction)moveRow:(id)sender {
    NSString *objToMove = @"Red";

    // Find the section that contains "Red"
    NSInteger sectionNum = [theData indexOfObjectPassingTest:^BOOL(NSArray *obj, NSUInteger idx, BOOL *stop) {
        return [obj containsObject:objToMove];
    }];

    // Find the row that contains "Red"
    NSInteger rowNum = [theData[sectionNum] indexOfObjectIdenticalTo:objToMove];

    if (sectionNum != NSNotFound && rowNum != NSNotFound) {
        [theData[sectionNum] removeObjectIdenticalTo:objToMove];
        [theData[1] insertObject:objToMove atIndex:0];
        [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:rowNum inSection:sectionNum] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM