UITableView

</pre><pre name="code" class="objc">- (void)loadView
{
    /**
     *  UITableView:表视图,是iOS中用来显示以及能够编辑一系列具有相同数据结构的信息列表的牛逼控件.(VIP).
        UITableView继承自UIScrollView,所以能够滑动,但是只能在垂直方向滑动,而且只有一列.
        UITableView是由分区(section,对应班级学生的分组),以及行(row,对应一个组的组员).对于分区和行的索引值都是从0开始的.
如果要获取某一个行(获取班里的某一个人),要根据分区以及所在分区的行(根据该学生所在的分组以及在组中的位置(第几个人))决定.
而这两个信息都存储在NSIndexPath类型的对象中.
        UITableView的每一行都是一个UITableViewCell类型的对象,每一个cell上都包含imageView(left),
label(textLabel,detailLabel,center),accessoryView(right)
     */
 UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    //设置分割线的样式
//    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    //设置分割线的颜色
    tableView.separatorColor = [UIColor lightGrayColor];
    //指定tableView的数据源.(为tableView显示提供数据支持). 需要服从UITableViewDataSource协议
    tableView.dataSource = self;
    //设置tableView的代理,(用来处理cell的点击事件).
    tableView.delegate = self;
    //设置tableView行高
    tableView.rowHeight = 70;
    
    self.view = tableView;
    [tableView release];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"9班";
}
#pragma mark - UITableViewDataSource
//设置tableView的行数(每个分组的行数)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    //1.先获取key值
//    NSString *key = self.titles[section];
//    //2.根据key值获取对应的数组
//    NSArray *group = self.names[key];
//    //3.求数组元素个数
//    return [group count];
//    return [self.names[self.titles[section]] count];
    return 1000;
}
//用来创建cell,每一行都要对应一个cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建重用标识符
    static NSString *identifier = @"7ban";
    //2.根据重用标识符去重用队列中取出可重用的cell.
    StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //3.判断是否成功取到可重用的cell.
    if (!cell) {
        //如果没有成功取到可重用的cell,就创建一个cell.
        //创建完cell之后一定要给cell贴一个重用标识符.方便别人重用.
        cell = [[[StudentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
    }
    cell.textLabel.text = @"憨豆";
    cell.detailTextLabel.text = @"熊猫";
    return cell;
    
    

//    //1.先获取key
//    NSString *key = self.titles[indexPath.section];
//    //2.根据key获取value(数组)
//    NSArray *group = self.names[key];
//    //3.从数组中取出元素
//    NSString *name = group[indexPath.row];
//    cell.textLabel.text = [[self.names objectForKey:[self.titles objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
}

//返回tableView的分区个数
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return [self.names count];
//}
设置每个分区头显示的文字.(页眉)
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return self.titles[section];
//}
//设置tableView右边的索引值,(用来快速定位分区,方便查找),要和每个分区的title对应上
//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
//{
//    return self.titles;
//}
#pragma mark - UITableViewDelegate
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //偶数的cell高度为100,奇数的cell高度为50
    if (indexPath.row % 2) {
        return 100;
    }
    return 50;
}
//当cell被选中时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d", indexPath.row);
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值