2013-01-01 279 views
0

在我的应用程序中,您可以点击一个单元格,并且它会调出一个新的视图。你可以编辑tableviewCell的textLabel,并保存它。但是,当您切换视图并返回到tableView时,单元格的顺序不同。这可能是什么原因?编辑单元格后,UITableView单元格被重新排序

这里是我的代码:

-(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; 
} 

感谢您的帮助

+0

代码的关键部分丢失。当用户改变某些东西时,你的代码做了什么修改tableArray?我要推测它在这个时候重新排列数组。 – Levi

回答

2

如果要保存的UITableView单元格,然后它会因为数组从您设置单元格的数据重新排序为初始后重装的tableView是因为它is.You只能更改表视图细胞不阵列数据位置数组。

下面我写一个UITableView码推view.If我从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 ([email protected]"Edit") { 
    [email protected]"Done"; 
    [_tableView setEditing:YES]; 
}else 
{ 
    [_tableView setEditing:NO]; 
    [email protected]"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 *[email protected]"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 

这是工作代码和推后不会改变视图控制器。

0

试试这个代码

-(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 
} 
+0

感谢或答复,但是我不能使用UITableViewCellStyleValue2,因为即时通讯使用自定义的UITableView Cell! –

+0

然后尝试在代码中将nil作为重用标识符传递。 – Abhishek

0

请写出下列代码可能对你有帮助!

-(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; 
}