博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
给TableView添加背景
阅读量:6928 次
发布时间:2019-06-27

本文共 3750 字,大约阅读时间需要 12 分钟。

iPhone SDK提供了默认的几个TableView样式,但是如果想提供更个性化的样式就需要自己定义。 比如添加背景

如上图的样子。 其实自定义table view的样子很简单,无非就是把table view和table view cell的背景变成透明的,然后在指定视图和cell的背景图片(当然,也可以指定table view的背景图片)

1234
@interface MainViewController : UIViewController 
{
UITableView *theTableView;}

先建立Controller,注意是继承自UIViewController而不是UITableViewController

实现类

1234567891011121314151617181920212223242526272829
- (id)init{
if (self = [super init]) {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];  // Setup the background UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]]; [self.view addSubview:background]; [background release];  // Create table view theTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 11, 320, 460) style: UITableViewStylePlain]; [theTableView setDelegate:self]; [theTableView setDataSource:self];  // This should be set to work with the image height [theTableView setRowHeight:68];  // Transparent, so we can see the background [theTableView setBackgroundColor:[UIColor clearColor]]; [theTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; [theTableView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];  [self.view addSubview:theTableView];  } return self;}

代码中的注释已经很清楚了。 先设置视图的背景,再设定table view的背景

再看另外一断代码,设置了cell的背景,注意,这里面使用了自定义的cell类CustomCell

123456789101112
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( *)indexPath {
CustomCell *cell= [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];  // Default to no selected style and not selected cell.selectionStyle = UITableViewCellSelectionStyleNone;  // Set the image for the cell [cell setTheImage:[UIImage imageNamed:[ stringWithFormat:@"Arrows%d.png", indexPath.row + 1]]];  return cell;}

我们再看看如何定义自定义的cell

12345678910
#import 
 @interface CustomCell : UITableViewCell {
UIImageView *image; } - (void) setTheImage:(UIImage *)icon; @end

再看实现类

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
#import "CustomCell.h" @implementation CustomCell /*---------------------------------------------------------------------------* *--------------------------------------------------------------------------*/-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:( *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Cells are transparent [self.contentView setBackgroundColor:[UIColor clearColor]]; }  return self;} /*---------------------------------------------------------------------------* *--------------------------------------------------------------------------*/- (void) setTheImage:(UIImage *) icon{
// Alloc and set the frame image = [[UIImageView alloc] initWithImage:icon]; image.frame = CGRectMake(0, 0, 286, 68);  // Add subview [self.contentView addSubview:image]; } /*---------------------------------------------------------------------------**--------------------------------------------------------------------------*/- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; if (selected == YES) image.alpha = .5; else image.alpha = 1;} /*---------------------------------------------------------------------------* *--------------------------------------------------------------------------*/- (void)dealloc {
[image release]; [super dealloc];} @end

转载于:https://www.cnblogs.com/pengyingh/articles/2348711.html

你可能感兴趣的文章
IIS 内部运行机制
查看>>
解决PL/SQL Developer连接数据库时出现 “ORA-12541:TNS:无监听程序”错误。
查看>>
关于完成端口IOCP异步接收连接函数AcceptEx注意事项 (转)
查看>>
Android 编程下两种方式注册广播的区别
查看>>
实现个hash_map容器类玩玩 - 苍梧 - 博客园
查看>>
Max Sum(经典DP)
查看>>
« 静态编译的MySQL易挂起 »
查看>>
关于 Oracle 索引以及 Bitmap 索引 和 B-tree 索引(归档)
查看>>
[zt]提问的艺术
查看>>
Global Cache CR Requested But Current Block Received
查看>>
How to use epoll? A complete example in C
查看>>
JScriptHelper类
查看>>
“万能数据库查询分析器”中英文4.02版本 2013-4-3日已在国内几大软件下载网站发布,敬请使用...
查看>>
memstr - Dustfly的专栏 - 博客频道 - CSDN.NET
查看>>
SSH无密码验证登录的实现(转摘)
查看>>
C# 修饰符的总结 default public private protected internal protectedinternal
查看>>
薛定谔之猫_百度百科
查看>>
jason数据格式详解
查看>>
公知_百度百科
查看>>
microsoft.sql.chainer.packagerdata.dll 0x84B10001解决方案
查看>>