分享

UITableView的基本使用一

 quasiceo 2014-04-16


   

       
        分类:
            IOS UI基础
       

    2014-02-06 20:40
    218人阅读
    评论(0)
    收藏
    举报
   



   




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条评论

    发表

    请遵守用户 评论公约

    类似文章 更多