2012-11-15 81 views
0

我使用兩個NSMutable Arrays來存儲多個複選標記。repeatDaysToStoredata1。 這是viewDidLoad方法的代碼...檢查UITableView中的多個單元格

for (int i=1; i<=[repeatDaysToStore count]; i++) 


{ 

    BOOL isTheObjectThere = [repeatArray containsObject:[repeatDaysToStore objectAtIndex:(i-1)]]; 

    if (isTheObjectThere) 
    { 

     NSString *into=[repeatArray objectAtIndex:[repeatArray indexOfObject:[repeatDaysToStore objectAtIndex:(i-1)]]]; 


       [data1 addObject:into]; 

     NSLog(@"check did load"); 

    } 



} 

,我使用檢查細胞DATA1陣列在此之後...

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell.textLabel.text=[repeatArray objectAtIndex:indexPath.row]; 
tableView.allowsMultipleSelection=YES; 
for (int j=1; j<=[data1 count]; j++) { 
    NSLog(@"%d",[[data1 objectAtIndex:(j-1)]intValue]); 
     NSLog(@"%d",indexPath.row); 
    if([data1 indexOfObject:[data1 objectAtIndex:(j-1)]]==indexPath.row) 
    { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    } 

和處理行選擇,我使用此代碼在didSelectRowAtIndexPath中 NSString * myobj = [repeatArray objectAtIndex:indexPath.row];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];  
     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [repeatDaysToStore removeObject:myobj]; 
     [data1 removeObject:myobj]; 


    } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [repeatDaysToStore addObject:myobj]; 
     [data1 addObject:myobj]; 

    } 
[tableView reloadData]; 
} 

但它不工作。有人請告訴我什麼是錯的?

+4

請描述「不工作」。有什麼症狀? –

+0

當我選擇一個單元格後,它的作品,當我選擇下一個單元格時,它什麼都不做,如果我點擊另一個單元格,它會選擇以前的單元格... – toofani

+0

我很困惑。 有沒有其他的方法來實現這一點? – toofani

回答

0

我認爲這個問題是cellForRowAtIndexPath:方法的下面的代碼:

for (int j=1; j<=[data1 count]; j++) 
{ 
    NSLog(@"%d",[[data1 objectAtIndex:(j-1)]intValue]); 
    NSLog(@"%d",indexPath.row); 
    if([data1 indexOfObject:[data1 objectAtIndex:(j-1)]]==indexPath.row) 
    { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
} 

刪除代碼,寫這樣的條件:

if([data1 containsObject:[repeatArray objectAtIndex:indexPath.row]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

還能去除[tableView reloadData];didSelectRowAtIndexPath:

+0

thnx很多... 但沒有必要刪除'[tableview reloadData]' – toofani

+0

@toofani:如果你寫'[tableview reloadData]'。它會在您選擇每個項目時重新加載tableView。這會導致性能問題。如果它沒有'[tableview reloadData]'工作。然後刪除該代碼:) –

+0

@Midhun ...但如果你刪除'[tableView reloadData]',那麼選擇只有在重新加載視圖後纔會更改...並且單元格變成藍色... – toofani

0

我想你需要一個不太複雜的數據結構來使這個工作易於理解。

您的表的數據應該是一個對象數組。每個對象至少應包含兩個屬性:一個是您要在cell.textLabel.text中顯示的信息,另一個是說明是否選擇該對象的BOOL屬性。

當選中某行時,將BOOL設置爲與當前所在行爲相反並重新加載表格。在cellForRowAtIndexPath:中,只需使用所請求行的對象的當前設置,而無需通過輔助數組。

相關問題