星空网 > 软件开发 > 操作系统

IOS——中级篇


//  设置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)[top与bottom无效]
    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];



原标题:IOS——中级篇

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

韩蓬_卖得易创始人:https://www.ikjzd.com/w/1635
梅星星:https://www.ikjzd.com/w/1636
苑萌:https://www.ikjzd.com/w/1637
孙琦:https://www.ikjzd.com/w/1638
周韬:https://www.ikjzd.com/w/1639
亚马逊二次审核_亚马逊二审:https://www.ikjzd.com/w/164
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流