iPhone應(yīng)用中給TableView添加背景
iPhone應(yīng)用中給TableView添加背景是本文呢要介紹的內(nèi)容,iPhone SDK提供了默認(rèn)的幾個(gè)TableView樣式,但是如果想提供更個(gè)性化的樣式就需要自己定義。 比如添加背景
如上圖的樣子。 其實(shí)自定義table view的樣子很簡(jiǎn)單,無(wú)非就是把table view和table view cell的背景變成透明的,然后在指定視圖和cell的背景圖片(當(dāng)然,也可以指定table view的背景圖片)
- @interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- {
- UITableView *theTableView;
- }
先建立Controller,注意是繼承自UIViewController而不是UITableViewController
- - (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;
- }
代碼中的注釋已經(jīng)很清楚了。 先設(shè)置視圖的背景,再設(shè)定table view的背景
再看另外一斷代碼,設(shè)置了cell的背景,注意,這里面使用了自定義的cell類(lèi)CustomCell
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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:[NSString stringWithFormat:@"Arrows%d.png", indexPath.row + 1]]];
- return cell;
- }
我們?cè)倏纯慈绾味x自定義的cell
- #import <UIKit/UIKit.h>
- @interface CustomCell : UITableViewCell
- {
- UIImageView *image;
- }
- - (void) setTheImage:(UIImage *)icon;
- @end
再看實(shí)現(xiàn)類(lèi)
- #import "CustomCell.h"
- @implementation CustomCell
- /*—————————————————————————
- *
- *————————————————————————–*/
- -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)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
還是很簡(jiǎn)單的吧。
小結(jié):iPhone應(yīng)用中給TableView添加背景的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)對(duì)你能有所幫助!