UITableView的基本使用一

UITableView继承自UIScrollView,所以内容过多的分屏操作,也不用开发者操心了,开发中很多数据展示都用到这个空间,比如类似九宫格菜单和系统设置,通讯录等等。

#pragma mark - 数据源方法 返回分组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

   return 2;

    

}

#pragma mark - 数据源方法 返回每组的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

   if (section==0) {

       return self.tencent.count;

    }else{

       return self.ali.count;

    }

}


#pragma mark 返回每组内容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

    

   NSString *city=nil;

   if (indexPath.section==0) {

        city=self.tencent[indexPath.row];

    }else{

        city=self.ali[indexPath.row];

    }

    //设置主标题

    cell.textLabel.text=city;

    

    //设置图标

    cell.imageView.image=[UIImageimageNamed:@"001.png"];

  

    //设置副标题

    cell.detailTextLabel.text=@"副标题";

    

    //设置右边的箭头样式

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

   return cell;

    

}

#pragma mark添加title

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

   if (section==0) {

       return @"腾讯";

    }else{

       return @"阿里巴巴";

    }

    

}

//添加脚信息

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

   if (section==0) {

        return@"腾讯旗下产品系列";

    }else{

        return@"阿里旗下产品系列";

    }

}


//UITableViewDelegate 设置每一行cell的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   return 80;

}

//显示索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    

    return @[@"a",@"b",@"b",@"c",@"d"];

}

//监听行

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    //弹框让控制器充当代理

    UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"title"message:@"提示"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

    // alert.tag=indexPath.row;

   self.currentRow=indexPath.row;

   self.currentSection=indexPath.section;

    alert.alertViewStyle=UIAlertViewStylePlainTextInput;

    

    //根据组和列获取lalbel的值

   NSString *text=nil;

   if(indexPath.section==0){

        text=self.tencent[indexPath.row];

    }else{

        text=self.ali[indexPath.row];

    }

    

    //设置文本框的值

    [alerttextFieldAtIndex:0].text=text;

    //显示弹出框

    [alertshow];

}


//alert点击了某个按钮

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    

   if (buttonIndex==1) {

     //根据tag获取

        

        //获取第一个文本框输入的值

       NSString *text=[alertView textFieldAtIndex:0].text;

       if(self.currentSection==0){

           self.tencent[self.currentRow]=text;

        }else{

            self.ali[self.currentRow]=text;

        }

        //刷新所有行数据

        //[self.dataView reloadData];

       NSIndexPath *path=[NSIndexPathindexPathForRow:self.currentRowinSection:self.currentSection];

        //带动画刷新指定行数据

        [self.dataViewreloadRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationLeft];

    }

    

}



总结:UITableView中的内容是通过UITableViewCell返回内容,而控件中经常配合使用的alertView很像easyui中的弹出框

numberOfSectionsInTableView

数据源方法 返回分组来源于代理UITableViewDataSource

numberOfRowsInSection

返回每组的行数来源于代理UITableViewDataSource

cellForRowAtIndexPath

返回每个cell的内容来源于代理UITableViewDataSource

titleForHeaderInSection

设置分组头title来源于代理UITableViewDelegate

titleForFooterInSection

设置分组脚title来源于代理UITableViewDelegate

heightForRowAtIndexPath

设置每行的高度来源于代理UITableViewDelegate

sectionIndexTitlesForTableView

设置每行的高度来源于代理UITableViewDelegate

didSelectRowAtIndexPath

监听选中行来源于代理UITableViewDelegate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值