2

我的应用程序中有 UITableView。

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

我想做的是,我想放一个图像(一个小条)作为表格的分隔符。

请帮我。

问候, 普拉蒂克

4

3 回答 3

23

对我有用的解决方案。

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];
于 2012-02-28T13:57:55.993 回答
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
于 2010-02-09T07:25:17.903 回答
0

我只想为安德鲁约翰逊的回答添加一些想法。

如果您将子视图添加到 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];
于 2012-01-18T05:52:46.427 回答