iOS开发,UITableView相关问题

mac2022-06-30  21

第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.count - 1 inSection:0]; [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];   //刷新指定cell NSIndexPath *indexPath_3=[NSIndexPath indexPathForRow:0 inSection:2]; NSArray *indexArray3=[NSArray arrayWithObject:indexPath_3]; [self.tableview reloadRowsAtIndexPaths:indexArray3 withRowAnimation:UITableViewRowAnimationAutomatic];     第二条:UITableViewCell分割线左边部分缺少一些的解决方法 -(void)viewDidLayoutSubviews {   if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {     [self.tableView setSeparatorInset:UIEdgeInsetsZero];   }   if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {     [self.tableView setLayoutMargins:UIEdgeInsetsZero];   } }   -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{       if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {     [cell setLayoutMargins:UIEdgeInsetsZero];   }   if ([cell respondsToSelector:@selector(setSeparatorInset:)]){     [cell setSeparatorInset:UIEdgeInsetsZero];   } }   第三条:UITableView的分割线部分不显示的问题? simulator -> debug -> optimize rendering for window scale 取消打勾就好     第四条: 自定义cell分割线大致用到的两种方法      a、把自定义的分割线当成一个View放到cell的contentView上,一定要注意重用问题,所以这个view 要在cell初始化的时候添加上。示例代码如下:  

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

      UITableViewCell *cell =nil;

      cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

      if (cell ==nil) {

          cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

          cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"huicellacce"]];  

          cell.backgroundColor = [UIColor clearColor];

          //cell.selected = YES;

          UIImageView *imageViewSepE = [[UIImageView alloc]initWithFrame:CGRectMake(47, 49, 200, 1)];

          imageViewSepE.image  = [UIImage imageNamed:@"godline"];

          [cell.contentView addSubview:imageViewSepE];     

      }

      return cell;

  }

    b、比较复杂,用到了底层的框架

  - (void)drawRect:(CGRect)rect {

      CGContextRef context = UIGraphicsGetCurrentContext();

      CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect);

      CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

      CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1)); //下分割线

      CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

      CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10,1));

  }

    第五条: 用代码的方式自定制cell,必须写initWithStyle的方法 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{       self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];      if (self) {               /*** 这里写要添加控件   ***/      }      return self; }      【iOS开发TableView】TabelView自定义cell   第一种:通过创建xib文件。   ①首先创建xib文件   ②创建xib文件对应 的模型A,名字与xib文件一样,并继承UITableViewCell类,并实现cellWithTableView的构造方法。   ③在interface builder里更改xib文件默认的类,为第二步创建的模型类。   ④创建数据模型B,并且A中包含数据模型B   ⑤A通过懒加载B方法(就是重写B对象的setter方法)将控件赋值。       第二种:代码自定义cell   ①新建一个继承UITableViewCell的类,里面拥有frame模型   ②重写initWithStyle:reuseIdentifier:方法(添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中,还有添加属性设置).   ③建立数据模型   ④建立frame模型(拥有数据模型)   ⑤重写数据模型对象的setter方法,然后再里面设置控件大小,和cell的高度。   ⑥控制器拥有frame对象数组。出事Cell的时候直接赋值给cell.frame对象就行。     第六条:iOS开发 - 让tableView不能下拉刷新,可以上拉加载 1. 首先遵循代理  UIScrollViewDelegate 2.实现代理方法即可 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{     if (tabelView.contentOffset.y <= 0) {         tabelView.bounces = NO;     }else {         tabelView.bounces = YES;     } }   第七条:去除UITableView底部多余行及分割线 self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];  

转载于:https://www.cnblogs.com/KennyHito/p/6860934.html

最新回复(0)