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);
}


原文地址:https://www.cnblogs.com/yjbjingcha/p/6893014.html