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

标签 ios objective-c uitableview

我将在 UITableView 中将一个单元格从一个部分移动到另一个部分。问题是我不知道这个单元格的索引路径。 (或者换句话说,我有这个单元格的索引路径,但索引路径现在可能已经过时了)。相反,我有一个指向此单元格的引用点。我怎样才能移动这个单元格?

提前致谢。

最佳答案

下面是一个示例,说明如何根据在单元格中找到特定字符串将行移动到第 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]];
    }
}

关于ios - 如何在 UITableView 中移动没有 NSIndexPath 的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677903/

相关文章:

iphone - 使用 AppDelegate 使变量对整个应用程序可用

ios - 无法同时滚动 UITableViewCell 中的 UIScrollView 和 UITableView 本身

iphone - 如何维护和清除 UIwebview 中的 session ?

ios - 呈现新 View Controller 时销毁以前的 View Controller

ios - 如何在Xcode中配置和运行 objective-c 测试用例--XCTest

ios - 如何将具有多个参数的 block 放入 NSOperationQueue?

ios - 具有关系的 Parse.com 查询

Objective-C/CGI 我唯一的选择?

ios - 让Cell根据TextInput出现和消失

iphone - 可以像 iOS5 Weather 应用程序一样向 UITableView 添加行吗?