本文主要是介绍tableview delegate 详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#pragma TableViewDelegate
分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataSource.count;
}
每组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.dataSource objectAtIndex:section]count];
}
cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 70;
}
else
{
return 44;
}
}
cell头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return 0;
}
else
{
return 25;
}
}
cell脚的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == 3 ) {
return 70;
}
else
{
return 0;
}
}
脚的自定义
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == 3) {
UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 70)];
UIButton *button1=[[UIButton alloc]initWithFrame:CGRectMake(35, 20, tableView.frame.size.width-35*2, 40)];
[button1 setBackgroundImage:[UIImage imageNamed:@"01确定按钮.png"] forState:UIControlStateNormal ];
[button1 setTitle:@"退出登录" forState:UIControlStateNormal ];
[button1 addTarget:self action:@selector(quite:) forControlEvents:UIControlEventTouchUpInside ];
[footView setBackgroundColor:[UIColor clearColor]];
[footView addSubview:button1];
return footView ;
}
else
{
return 0;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * identifier = @"identifier3";
setTableViewCell * cell = (setTableViewCell*)[[tableView dequeueReusableCellWithIdentifier:identifier]lastObject];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"setTableViewCell" owner:self options:nil]lastObject];
[cell setSelectionStyle: UITableViewCellSelectionStyleGray];
}
if (indexPath.section == 0)
{
cell.TX_image.frame = CGRectMake(20, 20, 40, 40);
cell.MX_label.frame = CGRectMake(70, 30, 200, 30);
cell.TP_image.frame = CGRectMake(SCREEN_WIDTH-30, 80/2-14/2 ,7, 14);
}
else
{
cell.TX_image.frame = CGRectMake(20, 20, 20, 20);
cell.MX_label.frame = CGRectMake(50, 10, 200, 30);
cell.TP_image.frame = CGRectMake(SCREEN_WIDTH-30, 44/2-14/2, 7, 14);
}
NSString * TX = [[[self.dataSource objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"TX"];
cell.TX_image.image = [UIImage imageNamed:TX];
cell.MX_label.text = [[[self.dataSource objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:@"MC"];
cell.TP_image.image = [UIImage imageNamed:@"setting_arrow.png"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
NSLog(@"啊哈0");
privateMessageViewController * pM = [[privateMessageViewController alloc]init];
[self.navigationController pushViewController:pM animated:YES];
}
else if (indexPath.section == 1)
{
if (indexPath.row ==0)
{
NSString * str = @"child";
[self childOrClass:str];
}
else
{
NSString * str = @"class";
[self childOrClass:str];
}
}
else if (indexPath.section == 2)
{
NSLog(@"啊哈2");
NotFriendlyViewController * notFriendly = [[NotFriendlyViewController alloc]init];
[self.navigationController pushViewController:notFriendly animated:YES];
}
else if (indexPath.section == 3)
{
if (indexPath.row == 0) {
privateSetViewController * privaSet = [[privateSetViewController alloc]init];
[self.navigationController pushViewController:privaSet animated:YES];
}
else if (indexPath.row == 1)
{
FeedBackViewController * feedBack = [[FeedBackViewController alloc]init];
[self.navigationController pushViewController:feedBack animated:YES];
}
}
}
这篇关于tableview delegate 详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!