ページ 11

UITableのセルに新着を表示させる。

Posted: 2012年12月20日(木) 21:25
by marucom
UITableのセルに新着ボタンを表示させ、一度選択したらそのボタンを非表示にする方法を教えてください。
表示させるボタンの生成は出来たのですが、選択後非表示に出来ません。
下記に新着ボタンのコードを記載します。



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"badegedCell";
TDBadgedCell *aCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (aCell == nil) {
aCell = [[TDBadgedCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identifier];
}


// 設定する内容をNSStringで渡す。
aCell.badgeString = [NSString stringWithFormat: @"新着"];
// 色の指定
aCell.badgeColor = [UIColor colorWithRed:0.8 green:0.2 blue:1.0 alpha:1.0];
// 丸みの指定
aCell.badge.radius = 9;
// 陰の指定
// Cell選択時にハイライトになった際に、陰を表示する。
aCell.showShadow = YES;

 return aCell;
}

Re: UITableのセルに新着を表示させる。

Posted: 2012年12月20日(木) 21:50
by h2so5
セルにバッジを表示するかどうかのフラグを作って、

コード:

if (flag) {
    aCell.badgeString = [NSString stringWithFormat: @"新着"];
    aCell.badgeColor = [UIColor colorWithRed:0.8 green:0.2 blue:1.0 alpha:1.0];
    aCell.badge.radius = 9;
    aCell.showShadow = YES;
} else {
    // バッジを表示しない
    aCell.badgeString = nil;
}
のようにしてバッジの表示を切り替えます。

選択した時の処理は didSelectRowAtIndexPath メソッドで設定できますから、
そこでフラグをOFFにしてTableViewをリロードすれば良いでしょう。

Re: UITableのセルに新着を表示させる。

Posted: 2012年12月20日(木) 22:28
by marucom
ありがとうございます。

>選択した時の処理は didSelectRowAtIndexPath メソッドで設定できますから、
そこでフラグをOFFにしてTableViewをリロードすれば良いでしょう。

とありますが、下記にどのような形でコードを記載てよいのかわかりません。
もしよろしければ、詳細をご返信いただければと思います。

[CODE

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {




if (indexPath.row == 0) {
Story1ViewController *svc = [[Story1ViewController alloc] initWithNibName:@"Story1ViewController" bundle:nil];
svc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:svc animated:YES];

}

if (indexPath.row == 1) {
Story2ViewController *s2vc = [[Story2ViewController alloc] initWithNibName:@"Story2ViewController" bundle:nil];
s2vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:s2vc animated:YES];

}
if (indexPath.row == 2) {
Story3ViewController *s3vc = [[Story3ViewController alloc] initWithNibName:@"Story3ViewController" bundle:nil];
s3vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:s3vc animated:YES];

}

if (indexPath.row == 3) {
Story4ViewController *s4vc = [[Story4ViewController alloc] initWithNibName:@"Story4ViewController" bundle:nil];
s4vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:s4vc animated:YES];

}


}
]

Re: UITableのセルに新着を表示させる。

Posted: 2012年12月20日(木) 22:43
by h2so5
まずバッジを表示するかどうかを管理するフラグを作りましょう。
配列をTableViewのメンバとして実装するというのはどうでしょうか。

また、上記のコードはもっと簡潔に書くことができます。

コード:

UIViewController *svc = nil;

switch (indexPath.row) {
    case 0:
        svc = [[Story1ViewController alloc] initWithNibName:@"Story1ViewController" bundle:nil];
        break;
    case 1:
        svc = [[Story2ViewController alloc] initWithNibName:@"Story2ViewController" bundle:nil];
        break;
    case 2:
        svc = [[Story3ViewController alloc] initWithNibName:@"Story3ViewController" bundle:nil];
        break;
    case 3:
        svc = [[Story4ViewController alloc] initWithNibName:@"Story4ViewController" bundle:nil];
        break;
}

svc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:svc animated:YES];

Re: UITableのセルに新着を表示させる。

Posted: 2012年12月22日(土) 21:33
by marucom
ご教授ありがとうございます。
記載していただいたコードですと、エラーが出てはしまいましたが、if文ではなくswichを使うというヒントをいただけましたので
活用させていただきます。