终于写到了UITableView,用处最广的一个控件,当然也是要记相当多东西的一个控件。

首选创建一个新的项目,并添加一个MainViewController的Class文件

IOS控件UITableView详解

打开MainViewController.h文件

  1. @interfaceMainViewController:UIViewController<UITableViewDataSource,UITableViewDelegate>
  2. @property(nonatomic,retain)NSArray*dataList;
  3. @property(nonatomic,retain)UITableView*myTableView;
  4. @end


TableView的数据源UITableViewDataSource

TableView的委托UITableViewDelegate

如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加

然后打MainViewController.m文件,初始化UItableView并显示在当前窗口

  1. -(void)viewDidLoad
  2. {
  3. [superviewDidLoad];
  4. //初始化tableView的数据
  5. NSArray*list=[NSArrayarrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津",nil];
  6. self.dataList=list;
  7. UITableView*tableView=[[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain]autorelease];
  8. //设置tableView的数据源
  9. tableView.dataSource=self;
  10. //设置tableView的委托
  11. tableView.delegate=self;
  12. //设置tableView的背景图
  13. tableView.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"Background.png"]];
  14. self.myTableView=tableView;
  15. [self.viewaddSubview:myTableView];
  16. }


在初始化的时候,可以为TableView设置样式

第一种:列表 UITableViewStylePlain

IOS控件UITableView详解

第二种:分组UITableViewStyleGrouped

IOS控件UITableView详解


创建并设置每行显示的内容

  1. -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. staticNSString*[email protected]"Cell";
  4. UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellWithIdentifier];
  5. if(cell==nil){
  6. cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:CellWithIdentifier];
  7. }
  8. NSUIntegerrow=[indexPathrow];
  9. cell.textLabel.text=[self.dataListobjectAtIndex:row];
  10. cell.imageView.image=[UIImageimageNamed:@"green.png"];
  11. [email protected]"详细信息";
  12. returncell;
  13. }
UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式

UITableViewCellStyleDefault

IOS控件UITableView详解

UITableViewCellStyleSubtitle

IOS控件UITableView详解

UITableViewCellStyleValue1

IOS控件UITableView详解

UITableViewCellStyleValue2

IOS控件UITableView详解


分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段

  1. -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
  2. {
  3. return1;
  4. }

设置内容缩进

  1. -(NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. return[indexPathrow];
  4. }
IOS控件UITableView详解

设置cell的行高

  1. -(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. return70;
  4. }
设置cell的隔行换色

  1. -(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. if([indexPathrow]%2==0){
  4. cell.backgroundColor=[UIColorblueColor];
  5. }else{
  6. cell.backgroundColor=[UIColorgreenColor];
  7. }
  8. }

IOS控件UITableView详解
当选择指定的cell时,弹出UIAlertView显示选择的内容

  1. -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. NSString*msg=[[NSStringalloc]initWithFormat:@"你选择的是:%@",[self.dataListobjectAtIndex:[indexPathrow]]];
  4. UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
  5. [msgrelease];
  6. [alertshow];
  7. }


IOS控件UITableView详解

滑动选择的行后删除

  1. -(void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. NSLog(@"执行删除操作");
  4. }

IOS控件UITableView详解


DEMO下载

http://pan.baidu.com/share/link?shareid=77810&uk=101519637

相关文章: