表控件 TableView
协议:UITableViewDataSource,UITableViewDelegate
委托函数:
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//返回表有几列。
{
return [self.data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//每一列分别是什么内容
{
NSString *SimpleTableIdentifier =@"SimpleTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell==nil){
cell=[[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];//identifier标识符号
//还可以根据tag获取控件
UILabel * title=(UILabel *)[cell viewWithTag:1];
}
UIImage *image=[UIImage imageNamed:@"123”];//图标
cell.imageView.image=image;
cell.detailTextLabel.text=@"你好!”;//注释
cell.textLabel.text=self.data[indexPath.row];
return cell; //返回类型是UITableViewCell *
}
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath//首行缩进函数
{
return indexPath.row;//返回几就缩进几格
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath//按下条目后的事件
{
detailViewController *controller=self.controller[indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
// 在代码中使用自己的table cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @“cellid";
UISpecialTableViewCell *cell = (UISpecialTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell= (UITwitterTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@“nib name" owner:self options:nil] lastObject];
}
// 自己的一些设置
return (UITableViewCell *)cell;
}
————————————————————————————————————————————————————————————————————
//设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0||indexPath.section==1||indexPath.section==2)
{
return 64;
}
else return 80;
}
————————————————————————————————————————————————————————————————————
//Nav
//详细按钮
-(void )tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
self.detailController.title=@"Disclosure Button Pressed";
NSString *selectedMovie =self.movies[indexPath.row];
NSString *detailMessage=[[NSString alloc] initWithFormat:@"this is details for %@",selectedMovie];
self.detailController.message=detailMessage;
self.detailController.title=selectedMovie;
[self.navigationController pushViewController:self.detailController animated:YES];
}
//设置Section的Footer
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
NSString *result = nil;
if ([tableView isEqual:myTableView]&§ion==0) {
result = @"Section 0 Header";
}
return result;
}
//点击后自动擦出cell痕迹
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//消除cell选择痕迹
[self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}
- (void)deselect
{
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}