移动UITableViewCell

时间:2016-07-22 08:55:25

标签: ios objective-c uitableview

大家好,我是iOS的新手。

我在UITableView中实施了UITableViewController 3个部分。我已经在每个部分显示了部分行。

 - (void)viewDidLoad 
  {
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = NO;

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

sectionTittle=[[NSMutableArray alloc]initWithObjects:@"Real Madrid",@"Barcelona",@"Liverpool", nil];
firstSection=[[NSMutableArray alloc]initWithObjects:@"Cr7",@"Garath Bale",@"Pepe",
              nil];
secondSection=[[NSMutableArray alloc]initWithObjects:@"Lionel Messi",@"Neymar",@"Pique",
              nil];
thirdSection=[[NSMutableArray alloc]initWithObjects:@"Gerrard",@"Saurez",@"Lallana",
               nil];


       }

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


if(section==0)
{
  return [firstSection count];
}
if(section==1)
{
    return [secondSection count];
}
if(section==2)
{
    return [thirdSection count];
}
return 0;





  }


     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hi" forIndexPath:indexPath];


cell.textLabel.textColor=[UIColor brownColor];



if (indexPath.section == 0)
{
    cell.textLabel.text=[firstSection objectAtIndex:indexPath.row];


}


if (indexPath.section == 1)
{
    cell.textLabel.text = [secondSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 2)
{
    cell.textLabel.text = [thirdSection objectAtIndex:indexPath.row];
}




// Configure the cell...
     return cell;
    }

     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  {

 return [sectionTittle objectAtIndex:section];
  }

  - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
  }

现在我的输出如下:

我想在这3个部分之间移动行。我该怎么做..?当我尝试这样做时,该行在该部分之间移动,但是保存时会产生问题。

我怎么能通过它??

由于

3 个答案:

答案 0 :(得分:0)

使用此代码

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSMutableArray *arrayTemp=[arrayMain objectAtIndex:sourceIndexPath.row];
    [arrayVisibleCountryList removeObjectAtIndex:sourceIndexPath.row];
    [arrayVisibleCountryList insertObject:arrayTemp atIndex:destinationIndexPath.row];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //Uncomment these lines to enable moving a row just within it's current section
    if ([sourceIndexPath section] != [proposedDestinationIndexPath section])
    {
        proposedDestinationIndexPath = sourceIndexPath;
    }

    return proposedDestinationIndexPath;
}

答案 1 :(得分:0)

您可以使用下面的tableView方法。它会将您的单元格从 sourceIndexPath 移动到 destinationIndexPath

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

答案 2 :(得分:0)

您需要利用UITableView重新订购容量。细胞可以跨部分重新排序。 要启用重新排序模式:

[self.tableView setEditing:TRUE animated:TRUE];

这使TableView处于编辑模式,包括启用删除模式和重新排序。如果您不想删除模式,请执行:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;

}

如果您已经实现了moveRowAtIndexPath,则会显示重新订购支持附件视图:

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

// Logic to change data source
NSMutableArray *deleteFrom = [self getDataSourceArrayFromIndexPath:sourceIndexPath];
NSMutableArray *addTo = [self getDataSourceArrayFromIndexPath:destinationIndexPath];
[addTo insertObject:[deleteFrom objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
[deleteFrom removeObjectAtIndex:sourceIndexPath.row];

}

其中getDataSourceArrayFromIndexPath为:

-(NSMutableArray*) getDataSourceArrayFromIndexPath:(NSIndexPath*) index
{
switch (index.section) {
    case 0:
        return self.firstSection;
        break;
    case 1:
        return self.secondSection;
        break;
    case 2:
        return self.thirdSection;
        break;
    default:
        break;
}
return nil;
}