2012-01-29 38 views
0

我有一些在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]; 

我做錯了什麼?

回答

2

你準確得到了什麼錯誤?看看你的代碼,但我認爲這個問題發生在你執行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

0

我不找你的錯誤,但我認爲你必須使用一個自定義的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