2

アプリケーションにUITableViewがあります。

属性:TableStyle =プレーン、SeperatorStyle =シングル、SeperatorColor=ブラック

私がやりたいのは、テーブルの区切り文字として画像(小さなストリップ)を配置したいということです。

私を助けてください。

よろしく、プラティック

4

3 に答える 3

23

私のために働いた解決策。

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];
于 2012-02-28T13:57:55.993 に答える
6

したがって、通常はiPhoneテーブルでは、組み込みのスタイルの1つを使用する必要があります:http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref / occ / instp / UITableView / SeparatorStyle

この道を歩んできたiPhone開発者として、可能な限り画像を避け、APIを使用してアプリを構築することをお勧めします。

あなたが無謀なコースを主張する場合、またはこれまたは何かを要求するクライアントがいる場合、あなたができることはサブクラスUITableViewCellです。

UITableViewCellサブクラスでは、セルの下部にある区切り画像など、必要なUI要素をセルのcontentViewに追加できます。

したがって、カスタムUITableViewCellを使用してテーブルセルを返すデリゲート関数の実装は次のとおりです。

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

  static NSString *CellIdentifier = @"Cell";

  // notice I use a SavedTableCell here instead of a UITavbleViewCell
  SavedTableCell *cell = (SavedTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
    cell = [[[SavedTableCell alloc] initWithReuseIdentifier:CellIdentifier hasIcon:YES] autorelease];
  }

  // custom function to set the fields in the cell
  [cell setItem:[self.items objectAtIndex:indexPath.row]];  
  return cell;
}

次に、SavedTableCell.hの簡略化された実装を示します。

#import <UIKit/UIKit.h>
#import "Item.h"

@interface SavedTableCell : UITableViewCell {

  // my cell has one label and one image, but yours would have an image placed at the bottom of the cell's frame
  UILabel *primaryLabel;
  UIImageView *icon;
}

// i just made up the class Item... in my code they are actually Waypoints.
- (void) setItem:(Item*)item;


@property(nonatomic,retain) UILabel *primaryLabel;
@property(nonatomic,retain) UIImageView *icon;


@end

そして、ここに簡略化されたSavedTableCell.mがあります。

#import "SavedTableCell.h"

@implementation SavedTableCell

@synthesize primaryLabel, icon;


- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
  if (self = [super initWithStyle:UITableViewStylePlain reuseIdentifier:reuseIdentifier]) {
    icon = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,40,40)];
    [self.contentView addSubview:icon];
    primaryLabel = [[UILabel alloc]init];
primaryLabel.font = TITLE_FONT;     
    [self.contentView addSubview:primaryLabel]; 
  } 
  return self;
}


- (void) setItem:(Item*)item {
  self.primaryLabel.text = [item name];
  [self.icon setImage:[item imageName]];
}

- (void)layoutSubviews {
  [super layoutSubviews];
  primaryLabel.frame = LABEL_FRAME;
  icon.frame = ICON_FRAME;  
}

- (void)dealloc {
  [icon release];
  [primaryLabel release];
}


@end
于 2010-02-09T07:25:17.903 に答える
0

アンドリュー・ジョンソンの答えにいくつかの考えを加えたいと思います。

サブビューをcontentViewに追加し、accesoryViewまたはimageを使用すると、セパレーター画像が正しく表示されません。次のようなものを使用します

UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
    bg.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
                    //or [UIColor colorWithImagePattern];
                    //or crete UIImageView and assign to backgroundView of the cell
self.backgroundView = bg;
[bg release];
于 2012-01-18T05:52:46.427 に答える