UItableView使用一

本文介绍如何使用 iOS 中的 UITableView 控件来实现分组显示数据的功能,包括设置样式、配置数据源、自定义单元格等关键步骤。
// 为控制器指定视图赋值
/**
 *  即;为self.view 赋值
 */
- (void)loadView
{
//    UITableViewStylePlain,   一行一行的(有线条)
//    UITableViewStyleGrouped  无线条(分区后就会显示)
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, 480) style:(UITableViewStylePlain)];
    // 分割线颜色
    tableView.separatorColor = [UIColor blueColor];
    // 分割线样式
//    UITableViewCellSeparatorStyleNone,
//    UITableViewCellSeparatorStyleSingleLine,
//    UITableViewCellSeparatorStyleSingleLineEtched
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置行高
//    tableView.rowHeight = 30;
    self.view = tableView;
    tableView.backgroundColor = [UIColor blueColor];
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView release];
}
/**
 *  数据
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.marry = [NSMutableArray arrayWithCapacity:3];
    NSArray *arry1 = @[@"张飞", @"刘备", @"关羽", @"曹操", @"赵云", @"张三", @"李四"];
    [_marry addObject:arry1];
    NSArray *arry2 = @[@"张三", @"刘四", @"关无", @"曹刘", @"赵三", @"张起", @"李大"];
    [_marry addObject:arry2];
    NSArray *arry3 = @[@"张1", @"刘1", @"关1", @"曹1", @"赵1", @"张1", @"李1"];
    [_marry addObject:arry3];
    self.headers = @[@"红楼梦", @"西游记", @"三国演义"];
    
}

- (void)didReceiveMemoryWarning
{
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource


/**
 *  划分Section的方法
 *
 *  @param tableView 表视图
 *
 *  @return 设置的section的个数(现执行这个方法)
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%s  %d", __FUNCTION__, __LINE__);
    return [self.marry count];
}

/**
 *  表示图每个分区的行数
 *
 *  @param tableView 表示图
 *  @param section   哪一个分区
 *
 *  @return 某个分区的row数
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    // 通过数组的方式设置有多少个区每个区有几个row
    return [[self.marry objectAtIndex:section] count];
}
/**
 *  设置每一个分区的row的元素数据
 *
 *  @param tableView 表示图
 *  @param indexPath cell所在的位置
 *
 *  @return 每个单元格的内容
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    // 定制可重用的cell单元格
    static NSString *cellWithIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellWithIdentifier];
    }
//    UITableViewCellAccessoryNone,
//    UITableViewCellAccessoryDisclosureIndicator, ( > )
//    UITableViewCellAccessoryDetailDisclosureButton,(导航加圈)
//    UITableViewCellAccessoryCheckmark,  (对号)
//    UITableViewCellAccessoryDetailButton (圈加叹号)
    // 辅助视图的类型
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 自定义现实的界面
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"darkLight.png"]];
    image.frame = CGRectMake(280, 0, 30, 30);
    cell.accessoryView = image;
    // 设置选中状态
//    UITableViewCellSelectionStyleNone,
//    UITableViewCellSelectionStyleBlue,
//    UITableViewCellSelectionStyleGray,
//    UITableViewCellSelectionStyleDefault
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    // 设置细节信息
    cell.detailTextLabel.text = @"hahahahah";
   
    // 取出对应的分区所含有的数据源
    NSArray *arry = [self.marry objectAtIndex:indexPath.section];
    // 通过行取出数据所在的位置
    NSString *title = [arry objectAtIndex:indexPath.row];
    cell.textLabel.text = title;
    
    return cell;
}
/**
 *
 *
 *  @param tableView 表示图
 *  @param section   所在的分区
 *
 *  @return 所在分区的头文件名
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 给每一个header赋值
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    return [self.headers objectAtIndex:section];
}
/**
 *  分区foot名
 *
 *  @param tableView 所在的表示图
 *  @param section   所在的分区
 *
 *  @return 每个分区的脚分区名
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSLog(@"%s  %d", __FUNCTION__, __LINE__);
    return @"大结局";
}







/**
 *  设置分区内容
 *
 *  @param tableView 表视图
 *
 *  @return 分区索引
 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    return self.headers;
}
/**
 *  设置头section的高度
 *
 *  @param tableView 表示图
 *  @param section   所指定的分区
 *
 *  @return 指定分区头文件的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
//    if (section == 1) {
//         return 50.0;
//    }else
//        return tableView.sectionHeaderHeight;
    return 50;
}
/**
 *  自定义头视图(通常和viewForHeaderInSection一块使用)
 *
 *  @param tableView 表示图
 *  @param section   指定的分区
 *
 *  @return 自定义的UIView
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
     NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    view.backgroundColor = [UIColor yellowColor];
    
    UISwitch *switc = [[UISwitch alloc]initWithFrame:CGRectMake(200, 10, 100, 30)];
    [view addSubview:switc];
    return [view autorelease];
}
/**
 *  选中某个单元格
 *
 *  @param tableView 表示图
 *  @param indexPath 索引信息
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s   %d", __FUNCTION__, __LINE__);
    NSString *str  = [NSString stringWithFormat:@"第%d个Section,第%d个row", indexPath.section, indexPath.row];
    NSArray *arry = [self.marry objectAtIndex:indexPath.section];
    NSString *title = [arry objectAtIndex:indexPath.row];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:str delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值