·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> UITableView的简单使用

UITableView的简单使用

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

UITableView分为两种style:UITableViewStyleGrouped和UITableViewStylePlain。

(一)UITableViewStyleGrouped

#import "ViewController.h"
//行高
#define cellHeight 60
//边距
#define margin 10
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *myTableView;
    NSArray *myData;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    //默认分隔线设置为无
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:myTableView];
    [self initWithData];
}
- (void)initWithData
{
    myData = [[NSArray alloc]init];
    myData = @[
               @{
                   @"city":@"北京",
                   @"cityfooter":@"我是北京",
                   @"district":@[@"朝阳区",@"海淀区"],
                   @"districtdetail":@[@"我是朝阳区",@"我是海淀区"],
                   @"image":@[@"1",@"2"],
                   },
               @{
                   @"city":@"上海",
                   @"cityfooter":@"我是上海",
                   @"district":@[@"徐汇区",@"闵行区",@"浦东新区"],
                   @"districtdetail":@[@"我是徐汇区",@"我是闵行区",@"我是浦东新区"],
                   @"image":@[@"1",@"2",@"3"],
                   },
               ];
}

#PRagma mark --------------tableviewDataSource---------------
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return myData.count;
}
//每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[myData[section] objectForKey:@"district"] count];
}
//组头
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [myData[section] objectForKey:@"city"];
}
//组尾
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return [myData[section] objectForKey:@"cityfooter"];
}
//设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellString = @"cellString";//cell的重用
    UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:cellString];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString];
        
        UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, margin, cellHeight-margin*2, cellHeight-margin*2)];
        cellImageView.backgroundColor = [UIColor brownColor];
        cellImageView.tag = 101;
        [cell addSubview:cellImageView];
        
        UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, margin, self.view.frame.size.width-(CGRectGetMaxX(cellImageView.frame)+margin), 20)];
        cellText.tag = 102;
        [cell addSubview:cellText];
        
        UILabel *cellDetailText = [[UILabel alloc]initWithFrame:CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+5, cellText.frame.size.width, 20)];
        cellDetailText.textColor = [UIColor lightGrayColor];
        cellDetailText.tag = 103;
        [cell addSubview:cellDetailText];
        //设置点选效果为无
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //设置行右边的箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //图标
    UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:101];
    cellImageView.image = [UIImage imageNamed:[myData[indexPath.section] objectForKey:@"image"][indexPath.row]];
    
    //标题
    UILabel *cellText = (UILabel *)[cell viewWithTag:102];
    cellText.text = [myData[indexPath.section] objectForKey:@"district"][indexPath.row];
    [self getHeightWithLabel:cellText andFontSize:15];

    //副标题
    UILabel *cellDetailText = (UILabel *)[cell viewWithTag:103];
    cellDetailText.text = [myData[indexPath.section] objectForKey:@"districtdetail"][indexPath.row];
    cellDetailText.frame = CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+5, cellText.frame.size.width, 20);
    [self getHeightWithLabel:cellDetailText andFontSize:13];
    
    //分隔线
    UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, cellHeight-1, self.view.frame.size.width, 1)];
    [lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
    [cell addSubview:lineImageView];
    
    return cell;
}
#pragma mark ---------------tableviewDelegate------------------
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这里写点击方法
    NSLog(@"点击了第%li组第%li行",(long)indexPath.section,(long)indexPath.row);
}
//UIlabel自适应高
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
    label.numberOfLines = 0;
    NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
    [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
    CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*2, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    CGRect labelRect = label.frame;
    labelRect.size.height = labelStringRect.size.height;
    label.frame = labelRect;
    label.attributedText = labelString;
}

(二)UITableViewStylePlain

#import "ViewController.h"
//行高
#define cellHeight 60
//边距
#define margin 10
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *myTableView;
    NSArray *myData;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    //默认分隔线设置为无
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:myTableView];
    [self initWithData];
}
- (void)initWithData
{
    myData = [[NSArray alloc]init];
    //此数组格式类似于网络开发用到过的Json格式
    myData = @[
               @{
                   @"city":@"北京",
                   @"district":@"朝阳区",
                   @"districtdetail":@"我是朝阳区",
                   @"image":@"1",
                   },
               @{
                   @"city":@"北京",
                   @"district":@"海淀区",
                   @"districtdetail":@"我是海淀区",
                   @"image":@"2",
                   },
               @{
                   @"city":@"上海",
                   @"district":@"徐汇区",
                   @"districtdetail":@"我是徐汇区",
                   @"image":@"1",
                   },
               @{
                   @"city":@"上海",
                   @"district":@"闵行区",
                   @"districtdetail":@"我是闵行区",
                   @"image":@"2",
                   },
               @{
                   @"city":@"上海",
                   @"district":@"浦东新区",
                   @"districtdetail":@"我是浦东新区",
                   @"image":@"3",
                   },
               ];
}

#pragma mark --------------tableviewDataSource---------------
//组数(UITableViewStylePlain样式默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myData count];
}
//设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellString = @"cellString";//cell的重用
    UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:cellString];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString];
        
        UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, margin, cellHeight-margin*2, cellHeight-margin*2)];
        cellImageView.backgroundColor = [UIColor brownColor];
        cellImageView.tag = 101;
        [cell addSubview:cellImageView];
        
        UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, margin, self.view.frame.size.width-(CGRectGetMaxX(cellImageView.frame)+margin), 20)];
        cellText.tag = 102;
        [cell addSubview:cellText];
        
        UILabel *cellDetailText = [[UILabel alloc]initWithFrame:CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+5, cellText.frame.size.width, 20)];
        cellDetailText.textColor = [UIColor lightGrayColor];
        cellDetailText.tag = 103;
        [cell addSubview:cellDetailText];
        
        UILabel *cellIntroduceText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellDetailText.frame)+margin, (cellHeight-20)/2, self.view.frame.size.width-(CGRectGetMaxX(cellDetailText.frame)+margin), 20)];
        cellIntroduceText.textColor = [UIColor darkGrayColor];
        cellIntroduceText.tag = 104;
        [cell addSubview:cellIntroduceText];
        //设置点选效果为无
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //设置行右边的箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //图标
    UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:101];
    cellImageView.image = [UIImage imageNamed:[myData[indexPath.row] objectForKey:@"image"]];
    
    //标题
    UILabel *cellText = (UILabel *)[cell viewWithTag:102];
    cellText.text = [myData[indexPath.row] objectForKey:@"city"];
    [self getHeightWithLabel:cellText andFontSize:15];

    //副标题
    UILabel *cellDetailText = (UILabel *)[cell viewWithTag:103];
    cellDetailText.text = [myData[indexPath.row] objectForKey:@"district"];
    cellDetailText.frame = CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+5, cellText.frame.size.width, 20);
    [self getHeightWithLabel:cellDetailText andFontSize:13];
    [self getWidthWithLabel:cellDetailText andFontSize:13];
    
    //介绍信息
    UILabel *cellIntroduceText = (UILabel *)[cell viewWithTag:104];
    cellIntroduceText.text = [myData[indexPath.row] objectForKey:@"districtdetail"];
    cellIntroduceText.frame = CGRectMake(CGRectGetMaxX(cellDetailText.frame)+margin, (cellHeight-20)/2, self.view.frame.size.width-(CGRectGetMaxX(cellDetailText.frame)+margin), 20);
    
    //分隔线
    UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, cellHeight-1, self.view.frame.size.width, 1)];
    [lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
    [cell addSubview:lineImageView];
    
    return cell;
}
#pragma mark ---------------tableviewDelegate------------------
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这里写点击方法
    NSLog(@"点击了第%li行",(long)indexPath.row);
}
//UIlabel自适应高
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
    label.numberOfLines = 0;
    NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
    [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
    CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*2, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    CGRect labelRect = label.frame;
    labelRect.size.height = labelStringRect.size.height;
    label.frame = labelRect;
    label.attributedText = labelString;
}
//UIlabel自适应宽
- (void)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
    label.numberOfLines = 0;
    NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
    [labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
    CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(9999, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    CGRect labelRect = label.frame;
    labelRect.size.width = labelStringRect.size.width;
    label.frame = labelRect;
    label.attributedText = labelString;
}