iPhone + UITableView +为分隔符放置图像

时间:2010-02-09 07:21:06

标签: iphone uitableview separator

我的应用程序中有UITableView。

属性:TableStyle = Plain,SeperatorStyle = single和SeperatorColor = black

我想要做的是,我想把一张图片(一个小条)作为桌子的分隔符。

请帮帮我。

此致 PRATIK

3 个答案:

答案 0 :(得分:23)

对我有用的解决方案。

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];

答案 1 :(得分:6)

因此,通常使用iPhone表格,您应该只使用其中一种内置样式: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

答案 2 :(得分:0)

我想在Andrew Johnson的回答中加入一些想法。

如果您将子视图添加到contentView并使用accesoryView或图像,那么它将错误地显示分隔符图像。使用像

这样的东西
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];