0

いくつかの UITableView を含むストーリーボードがあります。UITableViewCell の選択状態の別の表示タイプを選択したいと思います。私はよりよく説明します。

ハイライトのタイプを駆動する列挙型またはフラグが必要なコードに直行します。

-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
     switch(cellViewMode) {
       case Simple:
         [self simpleCell:selected];
         break;

       case Full:
         [self fullCell:selected];
         break;

       default:
         // default method...
         break; 
    }
}

ビューコントローラーのタイプに基づいて、この cellViewMode フラグを一度設定し、決して変更しないための最良の方法を探しています。実際には、識別子が宣言されているストーリーボードの通常の推奨方法でセルを呼び出していることを考慮してcellForRow: に静的な NSString *cellIdentifier があり、セルの存在をチェックする場合:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    Obj *obj = // get obj from datasource
    MyTableViewCell *cell = [table dequeueReusableCellWithIdentifier:MyTableCellIdentifier]; // <-- this is how it is done in storyboard
    cell.cellTitle.text=obj.titleForCell;
    cell.cellDescr.text=obj.descriptionForCell;

    return cell;
}

フラグが必要であると指定しましたが、カテゴリなどの他のソリューション、または適切でエレガントな方法で機能するものは何でも受け入れます。

4

1 に答える 1

0

少し調べた後、セル識別子プロパティを使用することでこれを簡単に解決できますが、識別子の性質上、'if' ステートメントを使用します。

-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
     if(self.reuseIdentifier==SimpleConstant) {
         [self simpleCell:selected];
         return;
     }

     if(self.reuseIdentifier==FullConstant) {
         [self fullCell:selected];
         return;
     }

     // handle other default case...
}

個人的には、このように使用される「if」ステートメントは好きではありませんが、より良い解決策はありませんでした。

于 2012-09-10T15:42:43.540 に答える