8

XIBファイルからカスタムUITableViewCellをロードするUITableViewがあります。すべてが正常に機能していますが、私のレイアウトでは、セル(すべて1つのセクション内)の間に間隔を空ける必要があります。ROWの高さを上げることなくこれを実行できる可能性はありますか?

今はどうですか
今はどうですか

どのようにそれが想定されているか
どのようにそれが想定されているか

編集:
これが今日のコードです

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.cards valueForKeyPath:@"cards"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    [ccTableView setBackgroundColor:[UIColor clearColor]];
    cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"];

    if(cell == nil){
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil];
      cell = [topLevelObjects objectAtIndex:0];
    }

    NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:indexPath.row];
    cell.descCardLabel.text = nmCard;

  return cell;
}
4

4 に答える 4

37

カスタムのuitableviewcellクラスを使用している場合、実際には非常に簡単な解決策が見つかりました。

セルクラスに、次のコードを追加します。

- (void)setFrame:(CGRect)frame {
    frame.origin.y += 4;
    frame.size.height -= 2 * 4;
    [super setFrame:frame];
}

これにより、このセルを呼び出すuitablviewクラスに配置したセルの高さ内に4ptのバッファーが提供されます。明らかに、ラベルや画像を配置するときにセル内のその少ないスペースを補正する必要がありますが、機能します。x軸でも同じことを行って、セルの幅を狭くすることもできます。セルの背後にある背景画像を表示するために、アプリでこれを行いました。

于 2014-03-31T02:10:17.460 に答える
7

セルの高さを変更できない場合、解決策は、必要な高さの非表示の中間セルを使用することです。その場合は、テーブルビューデリゲートとデータソースでインデックスを再計算する必要があります。

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

    static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
    // even rows will be invisible
    if (indexPath.row % 2 == 1)
    {
        UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];

        if (cell2 == nil)
        {
            cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:CELL_ID2];
            [cell2.contentView setAlpha:0];
            [cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
        }
        return cell2;
    }

    [ccTableView setBackgroundColor:[UIColor clearColor]];
    cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"];

    if(cell == nil){
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil];
      cell = [topLevelObjects objectAtIndex:0];
    }

     // Use indexPath.row/2 instead of indexPath.row for the visible section to get the correct datasource index (number of rows is increased to add the invisible rows)
        NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:(indexPath.row/2)];
        cell.descCardLabel.text = nmCard;

      return cell;
    }



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    // two times minus one (invisible at even rows => visibleCount == invisibleCount+1)
    return [[self.cards valueForKeyPath:@"cards"] count] * 2 - 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 1)
        return 40;
    return 162;
}

また、indexPath.rowfor:didSelectRowAtIndexPath:およびそれが使用される他のメソッドを再計算する必要があります。

于 2012-05-25T22:29:24.033 に答える
4

@ A-Liveは技術的には正しいですが、他の問題を防ぐために、たとえば、indexPath.row / 2をどこかに置くのを忘れた場合は、次のことができます(プログラミングは含まれません)。

たとえば、UITableViewCellの高さが通常90ポイントであり、各セルの間に10ポイントの間隔が必要だとします。UITableViewCellを100ポイント高くし、UITableViewCellの下部10ポイントを空白にします(いかなる種類のオブジェクトもありません)。次に、Interface BuilderでUITableViewCellをクリックし、backgroundColorを間隔領域の色にしたいものに設定します。

出来上がり!どこでも/2をプログラミングするよりもはるかに簡単な小さな作業で、各セルの間に間隔を空けることができます。

于 2013-07-28T03:33:41.520 に答える
0

あなたが迅速に解決策を探しているなら、redditからのこの答えは私にとって完璧に機能しました。

class CustomTableViewCell: UITableViewCell {
override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame =  newFrame
        frame.origin.y += 4
        frame.size.height -= 2 * 4
        super.frame = frame
    }
}
}
于 2016-08-12T10:15:55.860 に答える