我在我的应用程序中有UITableView。iPhone + UITableView +为分隔符设置图像
属性:TABLESTYLE =平原,SeperatorStyle =单和SeperatorColor =黑色
我想要做的是,我希望把一个图像(其中小的条带),作为隔板的表。
请帮帮我。
问候, PRATIK
我在我的应用程序中有UITableView。iPhone + UITableView +为分隔符设置图像
属性:TABLESTYLE =平原,SeperatorStyle =单和SeperatorColor =黑色
我想要做的是,我希望把一个图像(其中小的条带),作为隔板的表。
请帮帮我。
问候, PRATIK
所以,平时与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子类中,你可以添加你想要的单元格的contentView的任何UI元素,包括单元格底部的分隔符图像。
所以,这里是返回使用自定义的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
我想要理由给安德鲁约翰逊的答案增加一些想法。
如果您将子视图添加到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];
解决方案,为我工作。
self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];
注意colorWithPatternImage是从内存的使用角度来看是非常糟糕的,你在使用前应慎重考虑。有关更多信息,请参阅http://www.cocoaintheshell.com/2011/01/colorwithpatternimage-memory-usage/。在自定义表格单元中使用分隔图像的薄UIImageView可以很好地工作。 – 2012-09-19 09:12:36
@Andrew:通过子类UITableViewCell,你的意思是说“cellForRowAtIndexPath”方法??。另外,如何确定单元格的底部? – pratik 2010-02-09 07:29:43
@Andrew:你也可以详细讲述一下:“作为一直沿着这条道路的iPhone开发者,我建议你尽可能避免使用图片,并使用API来构建你的应用程序。” – pratik 2010-02-09 07:34:13
我添加了代码,显示如何执行自定义UITableViewCell。如果您想将单元格底部的图像作为分隔符,则只需适当地设置其框架。所以,如果你的表格单元是40像素高和320像素宽,那么你的图像的帧将是CGRectMake(0,40-image-size.height,image.size.width,image.size.height); – 2010-02-09 07:40:37