首先、對UITableView進行講解,下面有對它進行實際的應用
UITableView
顯示大型內容的列表
單行,多列
垂直滾動,沒有水平滾動
大量的數據集
性能強大,而且普遍存在於iPhone的應用程序中
TableView Styles
UITableView有兩個默認的內置風格,第一個是UITableViewStylePlain(簡明風格,簡明風格表明表格視圖自身沒有真正地在你自己實際地提供任何外觀之前提供很多的外觀,大部分情況下,它會做的唯一的事情是它會給你這些header和footer,在頂部的章節header有章節F,它是當前固定在屏幕的頂部,即使你滾動那個內容章節的header F會保持在那里,直到所有F的內容都移走,然后它滾出去。表格視圖還提供你在右邊的索引部分,那只用在簡易風格的表格視圖中,你能夠獲取它。)另一種風格是UITableViewStyleGrouped風格(組團風格),UITableViewStyleGrouped表格視圖是UIKit提供的另外一個默認風格,它給圍繞章節分段的外觀,它是垂直條紋的背景,頂部是白色圓形分段,你默認通過UITableView:StyleGrouped獲得該類型的外觀。
表格視圖以及組成部分
在頂部,我們有表格header,它是UITableView上的一個可設置的屬性,它只是個UIView,你能創建任何UIView子類或用UIImageView或UILable分配並設置它們為表格視圖的tableheader view ,然后表格視圖會安置它到你其余的內容之上,它會隨着內容上下滾動。
在表格視圖的內容底部,我們有表格footer,與header一樣是UIView的任意子視圖,它是你設置的UITableView的屬性,然后表格視圖會安置它到你其余的內容之下,在表格header和footer視圖之間的是章節內容。
在章節中,有章節的header和章節的footer,在它們之間是所有的獨立單元,就是Tablecell。如下圖:
使用TableViews
在表格視圖中顯示數據
定制顯示的外觀和行為
一個直接的解決方法
表格視圖用來顯示一列數據,所以使用一個數組
[myTableView setList : myListOfStuff];//該方法不存在,想法比較天真
該方法爭議性
所有的數據必須事先裝載
所有的數據都裝載在內存中
更加靈活的方案
另外一個對象為表格視圖提供數據
不是一次性全部取得
只取現在需要展示的數據
像一個委托,它是純粹的數據導向,它只向數據源詢問數據
UITableViewDataSource(數據源協議)
提供章節和行的數目
//optionalmethod ,default to 1 if not implemented
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
//requiredmethod
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
必要的時候向表格視圖提供所要顯示的cells
//required method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;//在該方法中,UITableViewCell是UIView的一個子類,它知道如何在表格視圖滾軸,知道如何繪制分組風格表格視圖和組群風格表格視圖,它必須自己是一個UITableViewCell或者UITableViewCell的子類。
NSIndexPath
是Foundation框架中的一個普通的類,它提供了到嵌套數列的樹中特定節點的路徑,事實上,它是一個整數陣列,表格視圖使用這個去表現在特定章節中的特定行,UITableView用的所有索引路徑正好有兩個元素,第一個是章節,第二個是行。
NSIndexPath和TableViews
@interfaceNSIndexPath (UITableView) {
}
+(NSIndexPath*)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
@property(nonatomic,readonly)NSUIntegerrow;
@property(nonatomic,readonly)NSUIntegersection;
@end
SingleSection Table View
返回行數
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return[myStringcount];
}
請求時提供一個單元
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell = ......;
cell.textLabel.text=[myStringobjectAtIndex:indexPath.row];
return[cell autorelease];
}
單元復用(cellreuse)
當你輕滑屏幕,你必須一次滾動上百行的文字,很多很多的不同單元,它不會完成的很好,每次一個消失,你破壞呢個表格視圖單元,然后為下一個創建新的,我們最終會做的是當一個表格視圖單元從頂部消失,它會放在一個復用隊列中,並且在你的TableViewCellforRowIndexPath方 法中,你可以選擇,當然你應該總是這樣做,你有機會去在分配新的單元前,向表格視圖詢問復用隊列其中的一個單元,如果一個單元已經從頂部滾軸消失,對於顯 示當前可視的東西,它不會必須的,你能取得它改變它的內容為你所需要顯示的新行而它會被重新滾進底部。(有點像循環瀑布)
-(UITableViewCell*)dequeueReusableCellWithindentifier:(NSString*)identifier;
//取回可復用的一個單元
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"MyIdentifier"];
if(cell== nil){
cell=[[[UITableViewCell alloc] initWithStyle:.....reuseIdentifier:@"MyIdentifier"]autorelease];
}
cell.text= [myStringobjectAtIndex:indexPath.row];
returncell;
}
觸發一個更新
什么時候數據源會被請求數據
當一行變得可視
完全更新會被響應當使用reloadData
-(void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated]; //所有的數據重新被加載,然后在放到復用隊列中
[self.tableViewreloadData];
}
章節和行進行重載數據
-(void)insertSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以插入整個章節
-(void)ideleteSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以刪除整個章節
-(void)reloadSections:(NSIndexSet*)sections withRowAnimation:
(UITableViewRowAniamtion)animation;//在iPhoneos 3.0中重載章節數據(上面兩個方法的合並)
它們能重載部分的數據,而不會把所有東西都丟掉
-(void)insertRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;
-(void)deleteRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;
-(void)reloadRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;
其它的數據源方法
為章節的header和footer設置標題(表格的header和footer是表格視圖上的屬性,你只分配那些視圖章節header和footer是依據章節的,因此它們由委派功能所提供)
允許編輯和重排單元
外觀和行為
UITableViewDelegate
可定制外觀和行為
邏輯獨立於視圖
數據源和委派經常是相同的對象(事實上,很少情況下數據源不是委派)
表格視圖的外觀和行為
定制表格視圖單元的外觀
-(void)tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath;
這個會在表格視圖的其中一個單元變為可視前立刻被調用,它是你在表格視圖單元顯示在屏幕上之前的外觀定制的最后機會,它保證一旦你調用這個方法,我們不會涉及任何單元的外觀,這之后,你改變的任何東西都會顯示在屏幕上,如果有一些東西你可能在TableViewcellForRowAtIndexPath方法中不知道,你想稍微懶惰地做這個,這會在那之后最后的機會去修改外觀。
驗證和響應所選擇的變化
使用表格視圖委派功能,對於選擇會有不同的功能
-(NSIndexPath*)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;//用戶選擇完成抬起手指,在該方法中,你可能決定去分配新的UIViewController,然后推它到self.navigationContrller,因此一些新事物滑進來,行選擇通常會觸發一個事件。選擇通常不是一個持續的事情,它通常不會在你輕敲之后保持選擇狀態,通常會談出選擇然后完成一個行為,或者你把它滑出屏幕,當然滑回來時會淡出。在iphone應用程序中你也會看到連續的選擇,我們在表格試圖中是支持持續選擇的,如果用戶選擇一行而你沒有反選它,我們不會替你反選,過去在iPhoneOS 2.0中不是這樣的,如果你不反選它,這個行為是不確定的,我們現在支持這個持續選擇行為。
響應選擇
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
NSIntegerrow = indexPath.row ;
idobjectToDispaly = [myObjects ObjectAtIndex:row ];
MyViewController* myViewController = …....;
myViewController.object = objectToDispaly ;
[self.navigationControllerpushViewController: myViewController animated:YES];
}
修改和禁用選擇
-(NSIndexPath*)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row== ….) {
returnnil;
}
else{ return indexPath ;}
}
UITableViewController
是UIViewController的子類,如果你想用全屏表格試圖風格,它會是很便利的起點,表格試圖會被自動創建,這包括控制器,表格視圖,數據源和委派,它在你第一次出現時替你調用reloadData,它在用戶導航回去時反選,會知道你之前所選的item。UITableViewController在正確的時間替你處理反選的事情,它還會閃爍滾動指示條,實際上這是iPhone人機界面的一部分。
TableView cells
指定的初始化程序
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier ;
//舊的版本,現在已經不是默認的初始化程序了
-(id)initWithStyle:(UITableviewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier ;
//新版本
單元風格(cellstyle)
基本屬性
UITableViewCell有一個圖像視圖和一到兩個的文本標簽
cell.imageView.image= [UIImage imageNamed:@”vitolidok.png”];
cell.textLabel.text= @”Vitol Idol”;
cell.detailTextLabel.text=@”Billy Idol”;
效果如圖:
如果你在表格視圖單元的默認風格上設置detailTextLabel.text,它是不會做任何事情,因為它不顯示一個額外的detailText。
附件類型
//UITableView委托方法
-(UITableViewCellAccessoryType)tableView:(UITableView*)table accessoryTypeForRowWithIndexPath:(NSIndexPath *) indexPath;
-(void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *) indexPath {
//只有是在藍色描述的按鈕(也就是上面的第二個)
NSIntegerrow = indexPath.row.
}
定制內容視圖
在有些情況下,簡單的圖像和文本是不夠的
UITableViewCell有一個contentview 屬性
為contentview 添加額外的視圖
-(UITableViewCell*) tableView :(UITableView) tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell*cell = ….......;
CGRectframe = cell.contentView.bounds;
UILabel*myLabel = [[UILabel alloc] initWithFrame :frame];
myLabel.text= ….;
[cell.contentViewaddSubview : myLabel];
[myLabelrelease];
}
其次,實際的使用方法
第一種,正常的使用方法
//
// TableViewViewController.m
// TableView
//
// Created by ch_soft on 11-11-7.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "TableViewViewController.h"
@implementation TableViewViewController
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
edit=NO;
[super viewDidLoad];
UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(10,10, 60,20);
[button addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"eidt" forState:UIControlStateNormal];
[self.view addSubview:button];
//創建一個tableview
tableview=[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 300, 400) style:UITableViewStylePlain];
[self.view addSubview:tableview];
tableview.delegate=self;
tableview.dataSource=self;
[tableview release];
}
-(void)edit:(id)sender
{
[tableview setEditing:!tableview.editing animated:YES];
if (edit) {
edit=NO;
[tableview setEditing:NO];
}else
{
edit=YES;
[tableview setEditing:YES];
}
}
#pragma mark TableView Delegate
//對編輯的狀態下提交的事件響應
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"commond eidting style ");
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
// Delete the row from the data source.
[tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
//響應選中事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"did selectrow");
}
//行將顯示的時候調用
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"will display cell");
}
//點擊了附加圖標時執行
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"accessoryButtonTappedForRowWithIndexPath");
}
//開始移動row時執行
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
NSLog(@"moveRowAtIndexPath");
}
//開發可以編輯時執行
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willBeginEditingRowAtIndexPath");
}
//選中之前執行
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willSelectRowAtIndexPath");
return indexPath;
}
//將取消選中時執行
-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willDeselectRowAtIndexPath");
return indexPath;
}
//移動row時執行
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
//用於限制只在當前section下面才可以移動
if(sourceIndexPath.section != proposedDestinationIndexPath.section){
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
//刪除按鈕的名字
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除按鈕的名字";
}
//讓表格可以修改,滑動可以修改
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//讓行可以移動
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//
NSLog(@"手指撮動了");
return UITableViewCellEditingStyleDelete;
}
#pragma mark TableView DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * indetify=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indetify];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify];
}
cell.textLabel.text=[dataArray objectAtIndex:indexPath.row];
cell.accessoryType=UITableViewCellAccessoryCheckmark;//添加附加的樣子
return cell;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
第二種,使用自定義UITbaleViewCell方法。
其實很簡單,只需要定義一個UItableViewCell的子類,然后在初始化cell的時候,使用自定義的cell生成cell就可以了。
@interface CustomTableCell :UITableViewCell {
}
@end
上篇文章介紹了如何用UITableView顯示表格,並講了幾種UITableViewCell的風格。不過有時候我們需要自己定義 UITableViewCell的風格,其實就是向行中添加子視圖。添加子視圖的方法主要有兩種:使用代碼以及從.xib文件加載。當然后一種方法比較直 觀。
我們這次要自定義一個Cell,使得它像QQ好友列表的一行一樣:左邊是一張圖片,圖片的右邊是三行標簽:
當然,我們不會搞得這么復雜,只是有點意思就行。
1、運行Xcode 4.2,新建一個Single View Application,名稱為Custom Cell:
2、將圖片資源導入到工程。為此,我找了14張50×50的.png圖片,名稱依次是1、2、……、14,放在一個名為Images的文件夾中。將此文件夾拖到工程中,在彈出的窗口中選中Copy items into…
添加完成后,工程目錄如下:
3、創建一個UITableViewCell的子類:選中Custom Cell目錄,依次選擇File — New — New File,在彈出的窗口,左邊選擇Cocoa Touch,右邊選擇Objective-C class:
單擊Next,輸入類名CustomCell,Subclass of選擇UITableViewCell:
之后選擇Next和Create,就建立了兩個文件:CustomCell.h和CustomCell.m。
4、創建CustomCell.xib:依次選擇File — New — New File,在彈出的窗口,左邊選擇User Interface,右邊選擇Empty:
單擊Next,選擇iPhone,再單擊Next,輸入名稱為CustomCell,選擇好位置:
單擊Create,這樣就創建了CustomCell.xib。
5、打開CustomCell.xib,拖一個Table View Cell控件到面板上:
選中新加的控件,打開Identity Inspector,選擇Class為CustomCell;然后打開Size Inspector,調整高度為60。
6、向新加的Table View Cell添加控件:拖放一個ImageView控件到左邊,並設置大小為50×50。然后在ImageView右邊添加三個Label,設置標簽字號,最上邊的是14,其余兩個是12:
接下來向CustomCell.h添加Outlet映射,將ImageView與三個Label建立映射,名稱分別為imageView、nameLabel、decLabel以及locLable,分別表示頭像、昵稱、個性簽名,地點。
選中Table View Cell,打開Attribute Inspector,將Identifier設置為CustomCellIdentifier:
為了充分使用這些標簽,還要自己創建一些數據,存在plist文件中,后邊會做。
7、打開CustomCell.h,添加屬性:
@property (copy, nonatomic) UIImage *image; @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *dec; @property (copy, nonatomic) NSString *loc;
8、打開CustomCell.m,向其中添加代碼:
8.1 在@implementation下面添加代碼:
@synthesize image; @synthesize name; @synthesize dec; @synthesize loc;
8.2 在@end之前添加代碼:
- (void)setImage:(UIImage *)img { if (![img isEqual:image]) { image = [img copy]; self.imageView.image = image; } } -(void)setName:(NSString *)n { if (![n isEqualToString:name]) { name = [n copy]; self.nameLabel.text = name; } } -(void)setDec:(NSString *)d { if (![d isEqualToString:dec]) { dec = [d copy]; self.decLabel.text = dec; } } -(void)setLoc:(NSString *)l { if (![l isEqualToString:loc]) { loc = [l copy]; self.locLabel.text = loc; } }
這相當於重寫了各個set函數,從而當執行賦值操作時,會執行我們自己寫的函數。
好了,現在自己定義的Cell已經可以使用了。
不過在此之前,我們先新建一個plist,用於存儲想要顯示的數據。建立plist文件的方法前面的文章有提到。我們建好一個friendsInfo.plist,往其中添加數據如下:
注意每個節點類型選擇。
9、打開ViewController.xib,拖一個Table View到視圖上,並將Delegate和DataSource都指向File’ Owner,就像上一篇文章介紹的一樣。
10、打開ViewController.h,向其中添加代碼:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> @property (strong, nonatomic) NSArray *dataList; @property (strong, nonatomic) NSArray *imageList; @end
11、打開ViewController.m,添加代碼:
11.1 在首部添加:
#import "CustomCell.h"
11.2 在@implementation后面添加代碼:
@synthesize dataList; @synthesize imageList;
11.3 在viewDidLoad方法中添加代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //加載plist文件的數據和圖片 NSBundle *bundle = [NSBundle mainBundle]; NSURL *plistURL = [bundle URLForResource:@"friendsInfo" withExtension:@"plist"]; NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL]; NSMutableArray *tmpDataArray = [[NSMutableArray alloc] init]; NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init]; for (int i=0; i<[dictionary count]; i++) { NSString *key = [[NSString alloc] initWithFormat:@"%i", i+1]; NSDictionary *tmpDic = [dictionary objectForKey:key]; [tmpDataArray addObject:tmpDic]; NSString *imageUrl = [[NSString alloc] initWithFormat:@"%i.png", i+1]; UIImage *image = [UIImage imageNamed:imageUrl]; [tmpImageArray addObject:image]; } self.dataList = [tmpDataArray copy]; self.imageList = [tmpImageArray copy]; }
11.4 在ViewDidUnload方法中添加代碼:
self.dataList = nil; self.imageList = nil;
11.5 在@end之前添加代碼:
#pragma mark - #pragma mark Table Data Source Methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.dataList count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; static BOOL nibsRegistered = NO; if (!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier]; nibsRegistered = YES; } CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowData = [self.dataList objectAtIndex:row]; cell.name = [rowData objectForKey:@"name"]; cell.dec = [rowData objectForKey:@"dec"]; cell.loc = [rowData objectForKey:@"loc"]; cell.image = [imageList objectAtIndex:row]; return cell; } #pragma mark Table Delegate Methods - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60.0; } - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { return nil; }
12、運行:
更改cell選中的背景
UIView *myview = [[UIView alloc] init];
myview.frame = CGRectMake(0, 0, 320, 47);
myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"0006.png"]];
cell.selectedBackgroundView = myview;