终于写到了UITableView,用处最广的一个控件,当然也是要记相当多东西的一个控件。
首选创建一个新的项目,并添加一个MainViewController的Class文件
打开MainViewController.h文件
- @interfaceMainViewController:UIViewController<UITableViewDataSource,UITableViewDelegate>
- @property(nonatomic,retain)NSArray*dataList;
- @property(nonatomic,retain)UITableView*myTableView;
- @end
TableView的委托UITableViewDelegate
如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加
然后打MainViewController.m文件,初始化UItableView并显示在当前窗口
- -(void)viewDidLoad
- {
- [superviewDidLoad];
- //初始化tableView的数据
- NSArray*list=[NSArrayarrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津",nil];
- self.dataList=list;
- UITableView*tableView=[[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain]autorelease];
- //设置tableView的数据源
- tableView.dataSource=self;
- //设置tableView的委托
- tableView.delegate=self;
- //设置tableView的背景图
- tableView.backgroundView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"Background.png"]];
- self.myTableView=tableView;
- [self.viewaddSubview:myTableView];
- }
第一种:列表 UITableViewStylePlain
第二种:分组UITableViewStyleGrouped
创建并设置每行显示的内容
- -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
- {
- staticNSString*CellWithIdentifier=@"Cell";
- UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellWithIdentifier];
- if(cell==nil){
- cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:CellWithIdentifier];
- }
- NSUIntegerrow=[indexPathrow];
- cell.textLabel.text=[self.dataListobjectAtIndex:row];
- cell.imageView.image=[UIImageimageNamed:@"green.png"];
- cell.detailTextLabel.text=@"详细信息";
- returncell;
- }
UITableViewCellStyleDefault

UITableViewCellStyleSubtitle

UITableViewCellStyleValue1

UITableViewCellStyleValue2

分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段
- -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
- {
- return1;
- }
设置内容缩进
- -(NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath
- {
- return[indexPathrow];
- }

设置cell的行高
- -(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath
- {
- return70;
- }
- -(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath
- {
- if([indexPathrow]%2==0){
- cell.backgroundColor=[UIColorblueColor];
- }else{
- cell.backgroundColor=[UIColorgreenColor];
- }
- }

当选择指定的cell时,弹出UIAlertView显示选择的内容
- -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath
- {
- NSString*msg=[[NSStringalloc]initWithFormat:@"你选择的是:%@",[self.dataListobjectAtIndex:[indexPathrow]]];
- UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
- [msgrelease];
- [alertshow];
- }
滑动选择的行后删除
- -(void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath
- {
- NSLog(@"执行删除操作");
- }
DEMO下载