单击行时如何将行移动到另一个部分?

时间:2015-08-27 10:52:49

标签: ios objective-c uitableview

我已经实现了多选行,但我要做的是当我们点击行时它会移动到另一个部分,这里是我的代码,我试过的是什么?

任何人都可以帮助我解决这个问题吗?提前谢谢......

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     return 2; //assuming you have only two sections
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     if (section == 0)
        return self.firstSectionDataSource.count;
     else if (section == 1)
        return self.dataArray.count;
     else
        return 0;
 }


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Here, Have to move row to another section while clcking ..

      // other multiselect code
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     // Configure a cell to show the corresponding string from the array.
    static NSString *kCellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];

    return cell;
}


 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
 {

   @try {

    NSMutableArray *sourceArray = nil;
    if (sourceIndexPath.section == 0) {
        sourceArray = self.firstSectionDataSource;
    }
    else if (sourceIndexPath.section == 1)
    {
        sourceArray = self.dataArray;
    }


    NSMutableArray *destinationArray = nil;
    if  (destinationIndexPath.section == 0) {
        destinationArray = self.firstSectionDataSource;
    }
    else if (destinationIndexPath.section == 1)
    {
        destinationArray = self.dataArray;
    }


    NSString *stringToMov = [sourceArray objectAtIndex:sourceIndexPath.row];
    [sourceArray removeObject:stringToMov];
    [destinationArray insertObject:stringToMov atIndex:destinationIndexPath.row];

  }
  @catch (NSException *exception) {
      NSLog(@"exception = %@ \n reason = %@",exception,exception.reason);
  }

}

2 个答案:

答案 0 :(得分:0)

你可以做下一个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];

    // other multiselect code
}

答案 1 :(得分:0)

试试这个 -

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"a",@"b",@"c", nil],[NSArray arrayWithObjects:@"d",@"e",@"f", nil], nil] forKeys:[NSArray arrayWithObjects:@"firstSection",@"secondsection", nil]];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return [[dict allKeys] count]; //assuming you have only two sections
}

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


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *arr = [dict objectForKey:[dict allKeys][indexPath.section]];
    NSString *str = arr[indexPath.row];

    if (indexPath.row!=0) {
        [arr removeObject:str];
    }

    NSMutableArray *arr1 = [dict objectForKey:[dict allKeys][0]];
    [arr1 addObject:str];
    [tableView reloadData];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [dict objectForKey:[dict allKeys][indexPath.section]];
    NSString *str = arr[indexPath.row];

    static NSString *kCellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    cell.textLabel.text = str;

    return cell;
}