天天看點

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];

        }

    }

}