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

发布于 2024-08-20 17:38:06 字数 176 浏览 6 评论 0原文

我的应用程序中有 UITableView。

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

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

请帮我。

问候, 普拉蒂克

I have UITableView in my application.

Attributes: TableStyle = Plain, SeperatorStyle = single and SeperatorColor = black

What I want to do is that, I want to put a image (which a small strip), as separator for the table.

Please help me.

Regards,
Pratik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌酱 2024-08-27 17:38:06

对我有用的解决方案。

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

Solution which worked for me.

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];
¢蛋碎的人ぎ生 2024-08-27 17:38:06

因此,通常对于 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

So, usually with an iPhone table, you should just use one of the built in styles: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/separatorStyle

As an iPhone developer who has been down this road, I suggest you avoid images wherever possible, and use the API to build your app.

If you insist on your reckless course, or if you have a client demanding this or something, then what you can do is subclass UITableViewCell.

In your UITableViewCell subclass, you can add whatever UI elements you want to the contentView of the cell, including a separator image at the bottom of the cell.

So, here is an implementation of the delegate function that returns a table cell using a custom 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;
}

And then here's my simplified implementation of 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

And here is a simplified 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
蓝天白云 2024-08-27 17:38:06

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

如果您将子视图添加到 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];

I want just to add some thoughts to Andrew Johnson's answer.

If you add subview to contentView and use accesoryView or image,then it'll show separator image incorrectly. Use something like

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文