UI之tableView的两个协议UITableViewDataSource、UITableViewDelegate

来源:互联网 发布:google网站数据统计 编辑:程序博客网 时间:2024/05/16 16:03

创建一个MangoViewController在其.h文件中引入UITableView数据源协议,代理协议(多个协议之间用' , '隔开)

@interface MangoViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

接下来,在其.m文件中写入代理和数据源的应用代码:

    self.view.backgroundColor = [UIColor orangeColor];
    //设置导航栏标题
    self.navigationItem.title = @"笑花";
    //创建一个UITableView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    //分割线颜色
    self.tableView.separatorColor = [UIColor blueColor];
    //分割线样式,默认是SingleLine样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //行高
    self.tableView.rowHeight = 88.0;
    //背景颜色
    self.tableView.backgroundColor = [UIColor magentaColor];
    //设置tableView数据源代理
   self.tableView.dataSource = self;
    self.tableView.delegate = self;

    //添加到当前视图上
    [self.view addSubview:self.tableView];
    [self.tableView release];
}
#pragma mark -------UITableViewDataSource(为了区分和方便理解,这里引入了标签#prama mark------)
//返回分区中有多少行,系统默认tableView有一个分区
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
//将要显示的cell在哪个位置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //重用机制实现原理
    //重用标示符 重用队列,出屏幕会被添加到重用队列,cell将要显示的时候先从重用队列里去取,如果重用队列是空的,就创建一个新的cell
    //定义一个重用标示符
    //static,静态区局变量,只需要创建一次,程序退出时释放
    static NSString *cellIdentifier = @"Cyclecell";
    //去tableView重用队列里取重用标识符为“Cyclecell”的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //cell == nil  当cell为空时,也就是重用队列里没有可用的cell,那么就创建一个新的cell
    if (cell == nil) {
        //切记,这里不能重新创建,而是继续使用上面创建好的cell
        //重用队列里没有可用的cell,重建一个,然后把新建的cell的重用标识符设置为上边定义的重用标示符
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    //insert code····
    //cell相关属性设置和赋值都可以在这里进行
    //设置背景颜色
    cell.backgroundColor = [UIColor magentaColor];
    //设置cell标题
    cell.textLabel.text = @"Winner";
    //里面有两个属性,一个是section区域,一个是row行
//    NSLog(@"%lu = %p",indexPath.row,&cell);
    if (indexPath.row == 0) {
        cell.textLabel.text = @"琪琪";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"俊俊";
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"茹茹";
    }else if (indexPath.row == 3){
        cell.textLabel.text = @"娟娟";
    }else if (indexPath.row == 4){
        cell.textLabel.text = @"豆豆";
    }else if (indexPath.row == 5){
        cell.textLabel.text = @"呆呆";
    }
    //设置副标题
//    cell.detailTextLabel.text = @"赢家";
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = @"星";
    }else if (indexPath.row == 1){
        cell.detailTextLabel.text = @"星星";
    }else if (indexPath.row == 2){
        cell.detailTextLabel.text = @"小星星";
    }else if (indexPath.row == 3){
        cell.detailTextLabel.text = @"星";
    }else if (indexPath.row == 4){
        cell.detailTextLabel.text = @"星星";
    }else if (indexPath.row == 5){
        cell.detailTextLabel.text = @"小星星";
    }
    //设置图片
    cell.imageView.image = [UIImage imageNamed:@"1.png"];
    //返回创建好的cell
    return cell;
}
//在TableView里有多少个分区,默认是一个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
//返回每个区头的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"窗寒西岭千秋雪";
    }else if (section == 1){
        return @"千山鸟飞绝";
    }
    return @"赢家";
}
//返回每个区尾的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return @"门泊东吴万里船";
    }else if (section == 1){
        return @"万径人踪灭";
    }
    return @"输家";
}
//返回右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSArray *titleArray = @[@"音乐",@"舞蹈",@"乐器"];
    return titleArray;
}
#pragma mark ------UITableViewDelegate(标签)
//设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
//设置区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44;
}
//自定义分区的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *blueV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    blueV.backgroundColor = [UIColor yellowColor];
    UILabel *label = [[UILabel alloc]initWithFrame:blueV.frame];
    label.text = @"你是谁啊";
    label.textAlignment = NSTextAlignmentCenter;
    [blueV addSubview:label];
    return blueV;
}
//自定义分区的尾部
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *blueV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    blueV.backgroundColor = [UIColor cyanColor];
    UILabel *label = [[UILabel alloc]initWithFrame:blueV.frame];
    label.text = @"傻瓜,你不认识我了么";
    label.textAlignment = NSTextAlignmentCenter;
    [blueV addSubview:label];
    return blueV;
}
//cell的选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSLog(@"****");//选中区域时控制台的输出结果
    }else if (indexPath.section == 1){
        NSLog(@"@@@@");
    }else if (indexPath.section == 2){
        NSLog(@"####");
    }
    NSLog(@"O,No");
}
//返回每一行的高度,rowHeight只能统一设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
             return 60;//设置第一分区第一行的高度为60,其他为44
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {//设置第二分区第一行的高度为100,其他为44
            return 100;
        }
    }
        return 44;
}



0 0
原创粉丝点击