2014-01-21 41 views
0

我正在使用引用爲'stateArray'的NSMutableArray。 stateArray需要簡單地保存我的單元格的BOOL值,以確定它們是否被選中。這裏是我的代碼..在NSMutableArray中使用BOOL對象來確定單元格狀態

stateArray在我的.h:

@property (nonatomic, strong) NSMutableArray *stateArray; 

然後stateArray 不是合成。需要在整個陣列中填寫NO,以便在選中單元格時,NO可以替換爲YES。目前此代碼正在爲每個單元格打印0的狀態陣列(NSLog位於我的cellForRowAtIndexPath:if (showCheckmark == YES)中)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:@"#669900"]]; 

    #define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)[email protected]"":str 

    static NSString *CellIdentifier = @"inviteCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //Customization 
    cell.textLabel.highlightedTextColor = [UIColor colorWithHexString:@"#669900"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.backgroundColor = [UIColor blackColor]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    //Ignore this, it's for UISearchBar 
    BOOL isSearching = tableView != self.tableView; 
    NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects); 
    id p = arrayToUse[indexPath.row]; 

    NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName)); 
    NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName)); 
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)]; 

    _stateArray = [NSMutableArray array]; 
    for (int i = 0 ; i != contactsObjects.count ; i++) [_stateArray addObject:@(NO)]; 

    BOOL showCheckmark = [[_stateArray objectAtIndex:indexPath.row] boolValue]; 

    if (showCheckmark == YES) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     NSLog(@"It hit showCheckmark = YES, and stateArray is %@",_stateArray[indexPath.row]); 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     NSLog(@"It hit showCheckmark = NO, and stateArray is %@",_stateArray[indexPath.row]); 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{  
    id object = contactsObjects[indexPath.row]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if (cell.accessoryType == UITableViewCellAccessoryNone) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [_stateArray replaceObjectAtIndex:indexPath.row withObject:@(YES)]; 
     [selectedObjects addObject:object]; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [_stateArray replaceObjectAtIndex:indexPath.row withObject:@(NO)]; 
     [selectedObjects removeObject:object]; 
    } 

    //slow-motion selection animation. 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
+1

您可以存儲[NSNumber](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsnumber_class/Reference/Reference.html),因爲您只能將對象存儲在NSMutableArray中和bool是原始數據類型,它有方法'+(NSNumber *)numberWithBool:(BOOL)值' –

+0

誰投了票,你能否改變這個。 – Chisx

+0

請拿掉投票。 – Chisx

回答

2

要跟蹤選定的項目來填充_stateArray,使用的Dictionary代替NSMutableArray並保持indexPath.row和選擇作爲對應的

而不是使用BOOL's數組進行這些操作,您可以更新代碼如下。

@property (nonatomic, strong) NSMutableDictionary * selectedRowCollection; 

- (void)viewDidLoad{ 

    self.selectedRowCollection = [[NSMutableDictionary alloc] init]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{  
    id object = contactsObjects[indexPath.row]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if (cell.accessoryType == UITableViewCellAccessoryNone) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [self.selectedRowCollection setObject:@"1" forKey:[NSString stringWithFormat:@"%d",indexPath.row]]; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [self.selectedRowCollection removeObjectForKey:[NSString stringWithFormat:@"%d",indexPath.row]]; 
    } 

    //slow-motion selection animation. 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    BOOL showCheckmark = [[self.selectedRowCollection valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]] boolValue]; 

    if (showCheckmark == YES) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
} 

注:不要忘記從詞典中刪除詞典項目,而重裝一個新的tableview數據集。

+0

好吧,你注意到我的其他問題。也就是說,我的cellForRowAtIndexPath不能識別單元格和indexPath.row是YES。換句話說,我在那裏的代碼沒有工作,它不記得哪些單元**被選中。出於某種原因'[_stateArray replaceObjectAtIndex:indexPath.row withObject:@(YES)];'不起作用 – Chisx

+0

其實我很抱歉,你回答了這個問題。這正是我所問的。爲什麼'BOOL'沒有這樣做? – Chisx

+0

我已經更新了我的答案.. –

1

BOOL值包裹着NSNumber,這就是爲什麼你得到的0:

_stateArray = [NSMutableArray array]; 
for (int i = 0 ; i != contactsObjects.count ; i++) [_stateArray addObject:@(NO)]; 

BOOL showCheckmark = [[_stateArray objectAtIndex:indexPath.row] boolValue]; 

if (showCheckmark == YES) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    NSLog(@"It hit showCheckmark = YES, and stateArray is %@",[[_stateArray objectAtIndex:indexPath.row] boolValue] ? @"YES" : @"NO"); 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    NSLog(@"It hit showCheckmark = NO, and stateArray is %@",[[_stateArray objectAtIndex:indexPath.row] boolValue] ? @"YES" : @"NO"); 
} 

BOOL沒有對象,所以你不能使用%@格式說明。您需要處理這個手動

0

試試這個,

更新您的循環由

for (int i = 0 ; i != contactsObjects.count ; i++) { 

    [_stateArray addObject:[NSNumber numberWithBool:NO]]; 
} 
相關問題