Objective c 如何正确设置分组TableView的背景图像?
我是iPhone开发的新手 我知道可能有一些答案与我的问题有关,但没有一个对我有帮助 根据标题,我将UITableView与样式分组。 我想设置背景图像,但无法正确设置 但问题是,正如我在截图中所描述的 我想只显示我的图像视图区域,该区域由Objective c 如何正确设置分组TableView的背景图像?,objective-c,uitableview,Objective C,Uitableview,我是iPhone开发的新手 我知道可能有一些答案与我的问题有关,但没有一个对我有帮助 根据标题,我将UITableView与样式分组。 我想设置背景图像,但无法正确设置 但问题是,正如我在截图中所描述的 我想只显示我的图像视图区域,该区域由UITableView的单元格描述(分组) 这是我的代码: self.tblView = [[UITableView alloc]initWithFrame:CGRectMake(0, 125, 320, 320) style:UITableViewStyl
UITableView的单元格描述(分组)
这是我的代码:
self.tblView = [[UITableView alloc]initWithFrame:CGRectMake(0, 125, 320, 320) style:UITableViewStyleGrouped];
UIImageView *bgTableImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 320)];
bgTableImageView.image = [UIImage imageNamed:@"bgImage.png"];
self.tblView.backgroundColor=[UIColor clearColor];
[self.tblView setBackgroundView:bgTableImageView];
//self.tblView.backgroundView = nil;
// self.tblView.opaque = NO;
//self.tblView.bounces = NO;
//self.tblView.scrollEnabled = YES;
self.tblView.delegate=self;
self.tblView.dataSource=self;
self.tblView.separatorColor = [UIColor darkGrayColor];
self.tblView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:self.tblView];
所有其他的工作都很好并且正常,我只有如何设置UITableViewStyleGrouped
?在使用UIImageView
时使用以下代码的问题
self.tblView.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]];
否则,
self.tblView.backgroundView.inputView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tblbg.png"]];
第二个选项(当您不依赖于UITableView
样式时,此选项很有用)
1) 更改您的UITableView
样式,样式:UITableViewStylePlain
2) 添加#导入框架
3) 更改UITableView的框架
例如,CGRectMake(10,“asyuneed”,300,“asyuneed”)
4) 给出您的圆角半径UITableView
self.tblView.layer.cornerRadius = 10; // set Radius as you need
并将UIImageView
设置为UITableView
的BackGroundView
。(遵循您在该问题中输入的代码)
上述步骤是用圆角创建的UITableView
我在这里提到当您不依赖UITableView
样式时使用此代码,如果TableView样式:Gropped
对您很重要,则此选项对您没有帮助
谢谢:)试试这个:
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"categorytab1.png"];
cell.backgroundView = av;
将UITableView
背景视图设置为nil以使其透明
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundImage.png"]]];
tableView.opaque = NO;
tableView.backgroundView = nil;
希望对你有帮助。你想要这样吗
代码如下:-
在任意位置设置UITableView的背景视图。我已在viewDidload中完成
[_tblView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"url.jpg"]]];
并在cellForRowAtIndexPath中将tableViewCellBackground color设置为透明
static NSString *cellIdentifier=@"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell ==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=@"goup styled";
return cell;
首先,介绍一下表的工作原理:
UITableView
有背景。这是你在细胞后面看到的
每个单元格都有三个特殊视图:contentView
,backgroundView
,selectedBackgroundView
backgroundView
显示在单元格内容下,当选中单元格时,将使用selectedBackgroundView
通常,所有内容都应转到contentsView
。背景与内容的分离使表格能够对未选定/选定的过渡设置动画。当单元格进入编辑模式时,这一点也很重要-内容被收缩/移动,编辑控件(例如删除按钮或单元格选择)可以独立显示在内容上
单元格还直接包含分隔符视图(通常在单元格底部显示1或2倍高的视图,具体取决于分隔符样式
)。这就是为什么单元格总是比其内容视图
高出至少1 px的原因
第二,关于分组表如何工作的一些信息
该表具有特殊的默认背景。您可以使用backgroundView
和backgroundColor
删除/更改它
分组表视图中的单元格具有较小的contentView
。单元格的宽度仍然与整个表格的宽度相同,但contentView
在左右两侧都有偏移(在iPhone上约为10点)
backgroundView
会被修改,它包括一个绘制边界的图层,具体取决于单元格位置(第一个、最后一个和中间单元格的位置不同)。边框的颜色由表的分隔颜色指定
您的代码中的所有内容都可以修改
删除边框的最简单方法之一是将separatorColor
设置为[UIColor clearColor]
。这将删除单元格分隔符,但您可以添加自己的分隔符,例如
cell = ...
UIView* separator = [[UIView alloc] init];
separator.backgroundColor = ...
separator.frame = CGRectMake(0.0f, table.bounds.size.width, table.rowHeight - 1.0f, 1.0f);
separator.autoresizingMask = (UIViewAutoresizingMaskFlexibleTopMargin | UIViewAutoresizingMaskFlexibleWidth);
[cell addSubview:separator]; //note we are adding it directly to the cell, not to contentsView
您还可以使用图像(UIImageView
)作为分隔符,而不是单色视图
另一种方法是将每个单元格的backgroundView
设置为nil
实际上,您可以完全忽略内容视图
,直接将所有内容添加到单元格中。然后,您可以自定义以下单元格方法,以便在单元格状态更改时更新单元格
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//update the cell text colors, backgrounds etc for the selected/not selected state
//you don't have to call super
// (the default implementation changes between backgroundView and selectedBackgroundView)
}
- (void)setHighlighted:(BOOL)highlited animated:(BOOL)animated {
//update the cell text colors, backgrounds etc for the selected/not highlighted state
//you don't have to call super
// (the default implementation changes between backgroundView and selectedBackgroundView)
//by giving empty implementation, you will block highlighting of cells
}
- (void)setEditing:(BOOL)highlited animated:(BOOL)animated {
//you can use this method to add custom editing controls
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
//use this method to change the layout of your contents
//when the cells goes into one of the editing states
}
- (void)layoutSubviews {
//update the frames of cell contents to match the new cell size
//note that the cell doesn't have the correct size until it is added to the table,
//this method is called when the cell already has the final size
//you can also use this method to change the size/position of the `contentsView`
}
编辑:
要解决您的具体问题,最好的解决方案可能是:
从表中删除默认的backgroundView
,并添加您自己的背景(例如,清晰颜色、白色、从图案创建的颜色或UIImageView
到backgroundView
)
从每个单元格中删除默认的backgroundView
,并将其替换为UIImageView
。第一个、最后一个和中间单元格需要三个特殊图像
如果要创建自定义单元格用户界面,请尝试使用
_tblNews.backgroundColor = [UIColor clearColor];
_tblNews.backgroundView = nil;
_tblNews.separatorColor = [UIColor clearColor];
_tblNews.separatorStyle = UITableViewCellSeparatorStyleNone;
并根据需要为单元格创建nib,并在数据源方法中加载该单元格
对于荷载xib
+ (id)loadNibNamed:(NSString *)NibName {
NSObject *cl = nil;
if (NSClassFromString(NibName) != nil) {
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:NibName owner:self options:nil];
for (id cls in arr) {
if([cls isKindOfClass:NSClassFromString(NibName)])
{
cl = cls;
break;
}
}
}
return cl;
}
确保在xib中为单元的可重用性设置标识符。
使用该功能,您可以轻松定制单元格布局。请尝试此功能
UIView *backgroundView = [[UIView alloc] init];
[backgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]]];
在tableview中,请设置此属性
UITableView *tableView = [[UITableView alloc] init];
[tableView setBackgroundView:backgroundView];
希望这对您有用。OP不想设置单元格的背景,但它想设置具有样式的TableView的背景。您的代码为行的每个单元格设置了imageview。我不想将UIImageView设置为单元格的背景,但我想将其设置为UITableView的背景,但是,您在添加的图像中说要显示图像v只查看分组表视图的单元格。在上面的评论中,您说您想查看表视图的背景?首先清除混淆。