IOS——中级篇 --TableView以及Cell

     
//  设置tableView的行高
   
self.tableView.rowHeight = 100;
//  设置tableView分割线的样式
//  UITableViewCellSeparatorStyleNone 不显示分割线
//  UITableViewCellSeparatorStyleSingleLine  显示分割线(默认)
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.allowsSelection = NO; // 不允许选中
//  设置分割线的颜色
   
self.tableView.separatorColor = [UIColor orangeColor];
   //  让没有内容单元格不显示[小技巧]
    self.tableView.tableFooterView = [[UIView alloc] init];
//  设置(top,left,bottom,right)[topbottom无效]
    self.tableView.separatorInset = UIEdgeInsetsMake(0,10, 0, 10);

在stroyborud 加载cell
    CZFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friend"];
    return cell;

    


dataSource 常用方法
/**
 *  多少个分组  numberOfSectionsInTableView 
 */
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

/**
 *  一个分组有多少行  numberOfRowsInSection
 */
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

/**
 *  每一个分组显示什么内容  cellForRowAtIndexPath
 */
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
//1创建Cell
static NSString *reusedId = @"item";
   
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId forIndexPath:indexPath];--------->wrong!!!!!
   
UITableViewCell *cell =[ tableView dequeueReusableCellWithIdentifier:reusedId ];
   
   
if (cell ==nil) {
        cell=[[
UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
    }
// 2.获取数据
    LYGroup *group = _groups[indexPath.section];
   
LYItem *item = group.items[indexPath.row];
    cell.
textLabel.text =item.title;
    cell.imageView.image =[UIImage imageNamed:item.icon];
//3.返回cell
     return cell;
}

/**
 *  分组(头部)标题 titleForHeaderInSection
 */
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
          return @“头部”;
}

/**
 *  分组(尾部)描述  titleForFooterInSection
 */
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
          return @“尾部”;
}
/**
 *  分组索引  sectionIndexTitlesForTableView
 */
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
   
//返回groups数组中,所有group对象的title属性,返回的是数组
   
return [self.carGroups valueForKeyPath:@"title"];
}

代理常用方法
#pragma mark - 代理方法
/**
 *  设置每一行的行高  heightForRowAtIndexPath
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
if (indexPath.row % 2 == 0) {
       
return 60;
    }
else{
       
return 100;
    }
}
/**
 *  已经选中某一行 didSelectRowAtIndexPath
 */
- (
void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//   zd long, long long ,int
    NSLog(
@"用户选中了第%zd,%zd",indexPath.section,indexPath.row);
}

//当取消选中某一行时候执行  didDeselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(
@"用户取消选中了第%zd,%zd",indexPath.section,indexPath.row);
}

CELL 常用属性
//  UITableViewCell *cell = [[UITableViewCell alloc] init];
//  UITableViewCellStyleDefault 只显示图标和名称
//  UITableViewCellStyleSubtitle 显示图标,名称,描述<下面>
//  UITableViewCellStyleValue1 显示图标,名称,描述<后面>
//  UITableViewCellStyleValue2 显示名称,描述<后面>
//  背景视图color
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor redColor];
// backgroundView的优先级高于backgroundColor
    cell.backgroundView = backView;
//  背景颜色
    cell.backgroundColor = [UIColor blueColor];

    UIView *selectedView = [[UIView alloc] init];
    selectedView.backgroundColor = [UIColor blueColor];
//  选中背景视图
    cell.selectedBackgroundView = selectedView;
   
//  指示器相关
//  设置指示器的类型
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//  指示器视图
    cell.accessoryView = [[UISwitch alloc] init];
/**
 *  缓存中取cell    //1创建Cell // 2.获取数据 //3.返回cell
 */
// 定义重用标识
    static NSString *reuseId =
@"heroCell";
   
// 去缓冲池中查找重用的cell
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
//  如果没有找到可以重用cell就创建新的cell
   
if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
//        NSLog(@"创建cell");
    }

//      用来刷新指定的行
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
 //      刷新表格
[self.tableView reloadData];

//      滚动让某个区域可见
[self.tableView scrollRectToVisible: (CGRect)rect animated:YES];

//滚动到哪一行
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tgs.count -1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
 
 // 第一组和最顶部的间距 headerView
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)];
    // 设置 组间距 15
    self.tableView.sectionHeaderHeight = 15;
   
self.tableView.sectionFooterHeight = 0;
   
   
//设置tableView的背景图片
   
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    //重写init方法,设置分组
  • - (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
 
UITableViewHeaderFooterView
重写  [[self alloc] initWithReuseIdentifier:reuseId];
UITableViewCell
重写 [[self alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];
posted @ 2015-06-25 19:43  邓小君  阅读(882)  评论(0编辑  收藏  举报