iOS中大流中的自定义cell 技术分享

简介:

AppDelegate.m指定根视图

[objc]  view plain  copy
  1. self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];  
//根视图

RootTableViewController.m

[objc]  view plain  copy
  1. #import "RootTableViewController.h"  
  2. #import "TestCell.h"  
  3. #import "TestModel.h"  
  4.   
  5. @interface RootTableViewController ()  
  6.   
  7. @property (nonatomicstrongNSMutableArray *datasourceArray;  
  8.   
  9. @end  
  10.   
  11. @implementation RootTableViewController  
  12.   
  13. - (void)viewDidLoad  
  14. {  
  15.     [super viewDidLoad];  
  16.       
  17.     self.datasourceArray = [NSMutableArray array];  
  18.       
  19.     [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"cell"];  
  20.       
  21.     for (int i = 0; i < 50; i++) {  
  22.         TestModel *model = [TestModel new];  
  23.         model.isShow = NO;  
  24.         [self.datasourceArray addObject:model];  
  25.     }  
  26.   
  27.       
  28. }  

#pragma mark - Table view data source

数据源方法
[objc]  view plain  copy
  1. #pragma mark - Table view data source  
  2.   
  3. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  4. {  
  5.     return 1;  
  6. }  
  7.   
  8. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  9. {  
  10.     // Return the number of rows in the section.  
  11.     return self.datasourceArray.count;  
  12. }  
  13.   
  14.   
  15. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  16. {  
  17.     TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];  
  18.       
  19.     TestModel *model = self.datasourceArray[indexPath.row];  
  20.       
  21.     if (model.isShow) {  
  22.           
  23.         cell.label.text = @"展示view";  
  24.         [cell addView];  
  25.     } else {  
  26.         cell.label.text = @"什么都没有";  
  27.         [cell removeView];  
  28.     }  
  29.   
  30.       
  31.     return cell;  
  32. }  

返回高

[objc]  view plain  copy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     TestModel *model = self.datasourceArray[indexPath.row];  
  4.     if (model.isShow) {  
  5.         return 300;  
  6.     } else {  
  7.         return 100;  
  8.     }  
  9. }  
点击cell触发的方法

[objc]  view plain  copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     TestModel *model = self.datasourceArray[indexPath.row];  
  4.     model.isShow = !model.isShow;  
  5.     [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  6.       
  7.     [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];  
  8. }  
准备一个自定义cell

[objc]  view plain  copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface TestCell : UITableViewCell  
  4.   
  5. @property (nonatomicstrongUILabel *label;  
  6. @property (nonatomicstrongUIView *redView;  
  7.   
  8. - (void)addView;  
  9. - (void)removeView;  
  10.   
  11. @end  
  12.   
  13. #import "TestCell.h"  
  14.   
  15. @implementation TestCell  
  16.   
  17. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  18. {  
  19.     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {  
  20.         [self addAllViews];  
  21.     }  
  22.     return self;  
  23. }  
  24.   
  25. - (void)addAllViews  
  26. {  
  27.     self.label = [[UILabel alloc] initWithFrame:CGRectMake(00, [UIScreen mainScreen].bounds.size.width100)];  
  28.     self.label.backgroundColor = [UIColor yellowColor];  
  29.     [self addSubview:self.label];  
  30.       
  31.     self.redView = [[UIView alloc] initWithFrame:CGRectMake(0100, [UIScreen mainScreen].bounds.size.width200)];  
  32.     self.redView.backgroundColor = [UIColor redColor];  
  33.       
  34. }  
  35.   
  36. - (void)addView  
  37. {  
  38.     [self addSubview:self.redView];  
  39. }  
  40.   
  41. - (void)removeView  
  42. {  
  43.     [self.redView removeFromSuperview];  
  44. }  
准备一个model类

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface TestModel : NSObject  
  4.   
  5. @property (nonatomic, assign) BOOL isShow;  
  6.   
  7. @end  

最终效果如下:




有好的建议和问题可微博私信:http://weibo.com/hanjunqiang

原文地址:http://blog.csdn.net/qq_31810357/article/details/49611255

相关文章
|
7月前
|
iOS开发
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
217 0
|
7月前
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
48 0
|
8月前
|
Swift iOS开发
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
257 0
|
Linux iOS开发 开发者
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
|
API iOS开发 Perl
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
|
API iOS开发 Perl
iOS UILabel自定义位置
iOS UILabel自定义位置
iOS UILabel自定义位置
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
268 0
|
移动开发 JavaScript weex
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
219 0
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
84 3