3

私の iPhone アプリは現在、2 つのセクションを持つグループ化されたテーブルを表示しています。次を使用して、各セクションの最初と最後のセル(または1行だけのセクションの場合は同じセル)の丸い角の半径を変更しようとしました:

cell.layer.cornerRadius = 4;

またはテーブルビュー全体に作用する:

self.tableView.layer.cornerRadius = 4;

しかし運が悪い...もちろん、この操作を実行する前にQuartzCoreをインポートしました。テーブルをプレーンスタイルで表示した場合のみ、コーナーをカスタマイズできるようです。

理想的な解決策は、最初の 1 つの上隅と最後の下隅をカスタマイズすることです。

助言がありますか?

4

7 に答える 7

3

最も簡単な方法は次のとおりです。

  1. xibファイルにカスタムセルを作成し、次のような一意の識別子を設定します@"beginCell", @"middleCell", @"endCell"。あなたはこのチュートリアルに従ってそれを作ることができます:http ://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

  2. メソッド - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath では、セクションの途中または最初/最後にいる場所を見つけて、適切な識別子を持つセルを使用する必要があります。

于 2012-09-19T11:38:56.653 に答える
2

できることはUIView、セルの背景に a を追加し、セルの背景を clearcolor にすることです。UIView角が丸くなるように調整します。

于 2012-09-19T09:48:38.930 に答える
1

丸みを帯びた角だけが必要な場合は、別の解決策があります(ネイティブの uitableview(cell) のグループ化されたスタイルに触発され、slowmo :) )。次のようになります。

セル サブクラス...

@interface MyCell : UITableViewCell
@property (nonatomic) top;
@property (nonatomic) bottom;
@end

@implementation MyCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor]; // remove white default background
        //example settings, UIImageViews with resizable images can be used too
        self.backgroundView = [[UIView alloc] init];
        self.selectedBackgroundView = [[UIView alloc] init];
        self.backgroundView.backgroundColor = [UIColor greenColor];
        self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
        self.backgroundView.layer.cornerRadius = self.selectedBackgroundView.layer.cornerRadius = 5; // 
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundView.frame = self.selectedBackgroundView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(self.top?0:-self.backgroundView.layer.cornerRadius, 0, self.bottom?0:-self.backgroundView.layer.cornerRadius, 0));
}
-(void)setTop:(BOOL)top{
    _top = top;
    [self setNeedsLayout];
}
-(void)setBottom:(BOOL)bottom{
    _bottom = bottom;
    [self setNeedsLayout];
}
@end

dataSource では、これを行うことができます:...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      ...cell initialization stuff...
      [(MyCell *)cell setTop:(indexPath.row == 0)];
      [(MyCell *)cell setBottom:(indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)];

      return cell;
}

長所:

  • すべてのセルに 1 つの背景
  • 使いやすい
  • アニメート可能(画像ではなく位置を変更しています)
  • プレーンスタイルにも使用できます(ただし、セクションに注意してください:))

短所:

  • cell のサブクラスが必要です (rowHeight を変更した後でも背景の位置を調整する方法が見つかりませんでした)
  • セクション全体の角が丸くなっているという問題は解決しません(セクションビューの角を手動で丸める必要があります)

これが誰かの役に立てば幸いですが、このコードはテストされていないことに注意してください! この回答を投稿する数分前にこのアイデアを実装しましたが、うまくいくようです:)

于 2013-11-28T01:23:10.483 に答える
1
UIView  *view = [UIView alloc]initWithFrame:YourFrame];
view.layer.cornerRadius = 8;

cell.backgroundView = view;

UITableView背景色が透明であることを忘れないでください。

于 2012-09-19T09:59:06.730 に答える