ページ 11

UITableViewでセルを選択したときのバッジ表示について

Posted: 2013年1月05日(土) 03:50
by marucom311
Xcode UITableView でセルを選択したときに未読と既読のバッジを表示するようにコードを作成しました。

現状、バッジの表示は出来るようになったのですが、1つのセルを選択するとすべてのセルが未読→既読になってしまいます。
セルごとに未読と既読を分けたいのですが....

何かアドバイスがありましたらご教授お願いいたします。

コード:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
   static NSString *CellIdentifier = @"Cell";
    
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    TDBadgedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
       // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        
    }
//セルの設定は割愛


//UserDefaultsから値を呼び出す
    NSUserDefaults *ud1 = [NSUserDefaults standardUserDefaults];  // 取得
    

    BOOL b = [ud1 boolForKey:@"KEY_B"];  // KEY_Bの内容をBOOL型として取得
    
   
    if (b == YES) {
        // バッヂ表示を設定する。
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        // 設定する内容をNSStringで渡す。
        cell.badgeString = [NSString stringWithFormat:@"既読"];
        // 色の指定
        cell.badgeColor = [UIColor blueColor];
        // 丸みの指定
        cell.badge.radius = 9;
        // 陰の指定
        // Cell選択時にハイライトになった際に、陰を表示する。
        cell.showShadow = YES;

                }
   
    if (b == NO) {
            // バッヂ表示を設定する。
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            // 設定する内容をNSStringで渡す。
            cell.badgeString = [NSString stringWithFormat:@"未読"];
            // 色の指定
            cell.badgeColor = [UIColor redColor];
            // 丸みの指定
            cell.badge.radius = 9;
            // 陰の指定
            // Cell選択時にハイライトになった際に、陰を表示する。
            cell.showShadow = YES;

        }


    
    
    return cell;
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        
   
    
    if (indexPath.row == 0) {
        NSUserDefaults *ud1 = [NSUserDefaults standardUserDefaults];  // 取得
        [ud1 setBool:YES forKey:@"KEY_B"];  // BOOL型のYESをKEY_Bというキーで保存
        [ud1 synchronize];
        // NSUserDefaultsに即時反映させる(即時で無くてもよい場合は不要)
        [tableView reloadData];
}
    


@end


Re: UITableViewでセルを選択したときのバッジ表示について

Posted: 2013年1月05日(土) 22:03
by Graiai
セル毎に異なるキーを使用するようにしてみては?