UITableView的移动

问题描述:

我有一些在NSMutableArray中的tableView包含类课程UITableView的移动

@interface Lesson : NSObject <NSCoding>{ 

    NSString *time1; 
    NSString *time2; 
    NSString *predmet; 
    NSString *namPrepod; 
    NSString *zamet; 
} 

我在tableview中移动排...................... .................................................. .................................................. ......

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    if(segmentedControl.selectedSegmentIndex==0) 
    { 
    if(fromIndexPath.section==0) 
    { 
     NSMutableArray *r = [[Singleton sharedInstance].chetNedel.monday objectAtIndex:fromIndexPath.row]; 
     [[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row]; 
     [[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row]; 
    } 
    if(fromIndexPath.section==1) 
    { 
     NSMutableArray *r = [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:fromIndexPath.row]; 
     [[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row]; 
     [[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row]; 
    } 

,但是当我在程序中移动排我有这个线错误

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     .......... 
if(segmentedControl.selectedSegmentIndex==0) 
    { 
     time1 = [[[UILabel alloc] init] autorelease]; 
     time1.font = [UIFont fontWithName:@"Arial-BoldMT" size:13]; 
     time1.tag = tagtime1; 
     time1.backgroundColor = [UIColor clearColor]; 
     if(indexPath.section ==0) 
      time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.monday objectAtIndex:indexPath.row] time1]; 
     if(indexPath.section ==1) 
      time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:indexPath.row] time1]; 
     if(indexPath.section ==2) 
      time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.wednesday objectAtIndex:indexPath.row] time1]; 
     if(indexPath.section ==3) 
      time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.thirsday objectAtIndex:indexPath.row] time1]; 
     if(indexPath.section ==4) 
      time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.friday objectAtIndex:indexPath.row] time1]; 
     if(indexPath.section ==5) 
      time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.saturday objectAtIndex:indexPath.row] time1]; 
     time1.frame = CGRectMake(8, 12, 300-124, 11); 
    ........... 
[cell.contentView addSubview:time1]; 

我做错了什么?

你准确得到了什么错误?看看你的代码,但我认为这个问题发生在你执行removeObjectAtIndex后跟insertObject:atIndex :.删除对象之后,将重新计算数组的索引,现在数组中的对象减少了1个。所以,如果你那么做insertObject:atIndex不降低,你想把它的索引,你不会,你想用它结束了。我建议你将两行合并为一行并使用exchangeObjectAtIndex:withObjectAtIndex:来代替。这里的更新代码:

if(fromIndexPath.section==0) 
{ 
    [[Singleton sharedInstance].chetNedel.monday exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; 
} 
if(fromIndexPath.section==1) 
{ 
    [[Singleton sharedInstance].chetNedel.tuesday exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; 
} 
+0

因未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayM time1]:无法识别的选择程序发送到实例0x600c2f0' – Pavel 2012-01-29 11:26:02

+0

此错误在此行上 time1.text = [(Lesson *)[[Singleton sharedInstance] .chetNedel .monday objectAtIndex:indexPath.row] time1]; – Pavel 2012-01-29 11:29:11

+0

它的确听起来像你可能正在访问一个已经失控的阵列位置。您是否尝试将removeObjectAtIndex和insertObject:atIndex:合并为一个exchangeObjectAtIndex:withObjectAtIndex:就像我建议的那样? – ragamufin 2012-01-29 21:56:34

我不找你的错误,但我认为你必须使用一个自定义的UITableViewCell,而不是在每一行添加一个UILabele ......因为细胞被回收和时间1可能已经存在。

+0

我该怎么做?示例请 – Pavel 2012-01-29 14:03:47

+0

此链接http://howtomakeiphoneapps.com/how-to-design-a-custom-uitableviewcell-from-scratch/1292/可以帮助你。 – 2012-01-30 09:59:19