TableView

本文详细介绍了如何使用Swift和UIKit开发一个具有分区、表格视图和数据展示功能的通讯录应用,包括设置代理、数据源,实现表格视图的布局和交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  RootViewController.m


#import "RootViewController.h"

#import "SecondViewController.h"

 

@interfaceRootViewController()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>

@property (nonatomic,strong)NSArray *arr;

@property(nonatomic,strong)NSMutableDictionary *dic;

@property (nonatomic,strong)UITableView*tableView;

 

@end

 

@implementationRootViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

 

self.title = @"通讯录";

self.view.backgroundColor =[UIColoryellowColor];

UITableView *tableView =[[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStylePlain];

 

self.tableView = tableView;

tableView.backgroundColor =[UIColorredColor];

   // 设置分割线颜色

tableView.separatorColor =[UIColorgreenColor];

   // 设置分割线样式

//   tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //设置行高

tableView.rowHeight = 100;

   // 设置表视图的代理

   // datasource主要是提供数据,数据源的代理。

   // delegate 主要是外观之类的设置

tableView.dataSource = self;

tableView.delegate = self;

   [self.viewaddSubview:tableView];

NSMutableArray *arr1 = [NSMutableArrayarrayWithObjects:@"张尼玛",@"土耳其",@"普京",nil];

NSMutableArray *arr2 =[NSMutableArrayarrayWithObjects:@"赵日天",@"叶良辰",@"吴栋生",@"挑fen男孩",@"隔壁老王",nil];

 

//   self.arr = @[arr1,arr2];

NSMutableDictionary *dictionary =[NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"A",arr2,@"B",nil];

self.dic = dictionary;

self.arr =@[@"A",@"B"];

 

}

        // 设置分区的行数

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

returnself.arr.count;

//   return 2;

}

 

   // 数据源的协议方法(有多少个分区就进入这个方法多少次,每次获取一个分区下标.)

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

//   if(section == 0){

//       // 根据对应分的数组中的元素个数决定单元格个数

//       //return  [self.arr[section]count];

////       return 3;

//

//   }else{

//      // return [self.arr[section] count];

////       return 5;

//   }

        //  设置每个分区的单元格个数+

       //  self.arr[section] 根据分区获得key值,self.dic[]根据key值获得数组

return [self.dic[self.arr[section]]count];

}

 

   //配置单元格数据

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

   // 定义一个重用标识符

staticNSString *reuse = @"log";

     // 从重用队列中取出cell

UITableViewCell *cell =[tableViewdequeueReusableCellWithIdentifier:reuse];

   // 重用队列中存放的格式一样的cell,内容不一样,创建的单元格总共为屏幕可容纳的cell个数,前面的内容不在屏幕内后,cell就进入重用队列中,这时重用队列中的cell用来显示后面进入屏幕的内容。

    // 从重用队列中取出可用(重用标识符一样)的cell

   // 如果cell为空,(重用队列中无此cell)

if (cell == nil) {

       // 新建一个cell

cell =[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:reuse];

NSLog(@"没有此单元格,新建一个单元格");

}else{

NSLog(@"这时重用的单元格");

    }

cell.imageView.image =[UIImageimageNamed:@"h2.jpeg"];

 

   // NSIndexPath有两个属性:

   // 通过indexpath.row访问单元格

   // 通过indexPath.section访问分区

 

   // 随着屏幕的滚动indexPath会变化

    //用indexPath.section获取到当前分区,indexPath.row获取到当前单元格。

//   cell.textLabel.text = self.arr[indexPath.section][indexPath.row];

//   cell.textLabel.text = self.arr[indexPath.row];

 

 

//   // 首先拿到对应分区的key值

//   NSString *key = self.arr[indexPath.section];

//    // 根据key值获取对应分区的数组

//   NSArray *arr = [self.dicvalueForKey:key];

//   // 通过数组为单元格赋值

//   cell.textLabel.text = arr[indexPath.row];

 

cell.textLabel.text =self.dic[self.arr[indexPath.section]][indexPath.row];

//   cell.detailTextLabel.text = @"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值