简体   繁体   English

编辑单元格后,UITableView Cells将重新排序

[英]UITableView Cells get reordered after editing the cell

In my app, You can tap a cell, and it brings up a new view. 在我的应用程序中,您可以点击一个单元格,它会显示一个新视图。 You can edit the tableviewCell's textLabel, and save it. 您可以编辑tableviewCell的textLabel,并保存它。 But when you switch views and come back to the tableView, the cells are in a different order. 但是当您切换视图并返回到tableView时,单元格的顺序不同。 What could be the reason for this? 这可能是什么原因?

Here is my Code: 这是我的代码:

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

static NSString * identifier = @"identifier";

self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];

h = [self.tableArray objectAtIndex:indexPath.row];

NSString *brandModel = [[h.brand stringByAppendingString:@" "] stringByAppendingString:h.model];

self.cell.mainLabel.text = brandModel;

self.cell.nicknameLabel.text = handguns.nickname;   

return self.cell;
}

Thanks for the Help 谢谢您的帮助

If you are reloading tableView after saving UITableView cell then It will be reordered as initial because array from which you are setting cell data is as it is.You can change only table View cells not array data position in an array. 如果您在保存UITableView单元格后重新加载tableView,那么它将被重新排序为初始值,因为您要设置单元格数据的数组就是原样。您只能更改表格查看单元格而不是数组中的数组数据位置。

Below I have written a UITableView code to move cells and it is working fine after pushing view.If I remove reload method from viewWillAppear: 下面我编写了一个UITableView代码来移动单元格,并且在推送视图后它工作正常。如果我从viewWillAppear中删除重载方法

#import "MoveCellViewController.h"

@interface MoveCellViewController ()

@end

@implementation MoveCellViewController

- (void)viewDidLoad
{
 UIBarButtonItem *left=[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
 self.navigationItem.leftBarButtonItem=left;

 myArray=[[NSMutableArray alloc]init];  //mutable array;
 myArray=[NSMutableArray arrayWithObjects:@"Aman",@"Ankit",@"Sumit",@"Hercules",@"Jackie Chan", nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)edit
{
 if (self.navigationItem.leftBarButtonItem.title==@"Edit") {
    self.navigationItem.leftBarButtonItem.title=@"Done";
    [_tableView setEditing:YES];
 }else
 {
  [_tableView setEditing:NO];
  self.navigationItem.leftBarButtonItem.title=@"Edit";
 }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return [myArray count];
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

 if (cell==nil) {
  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }

 cell.userInteractionEnabled=YES;
 cell.textLabel.text=[myArray objectAtIndex:indexPath.row];
 return cell;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
 return YES;
}

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

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
 id ob = [myArray objectAtIndex:destinationIndexPath.row];

 [myArray replaceObjectAtIndex:destinationIndexPath.row withObject:[myArray objectAtIndex:sourceIndexPath.row]];
 [myArray replaceObjectAtIndex:sourceIndexPath.row withObject:ob];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 NextViewController *nx=[[NextViewController alloc] init];
 [self.navigationController pushViewController:nx animated:YES];
}
@end

This is working code and will not change after push view controller. 这是工作代码,在推视图控制器后不会改变。

Try this code 试试这个代码

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     static NSString *CellIdentifier = @"Cell";

     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


     if (cell == nil)
     {

         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2     reuseIdentifier:nil];
     }

     Write rest of the code here
}

Please write following code May be helpful for you!!! 请写下面的代码可能对您有所帮助!

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

 NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone; 
   }
h = [self.tableArray objectAtIndex:indexPath.row];

NSString *brandModel = [[h.brand stringByAppendingString:@" "] stringByAppendingString:h.model];

self.cell.mainLabel.text = brandModel;

self.cell.nicknameLabel.text = handguns.nickname;   

return self.cell;
}

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

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