2013-01-01 209 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; 
}