UITabview

  1. //********************************************************************************
  2. //*自定义类 UITableViewCell START
  3. //********************************************************************************
  4. //静态变量声明定义
  5. static NSString * __nonnull cellID=@"itemCellId";//可复用的reuseIdentifier
  6. @interface MItemCellView : UITableViewCell
  7. @property (weak, nonatomic) IBOutlet UITextField *titleTV;
  8. @property (weak, nonatomic) IBOutlet UITextField *descTV;
  9. + (MItemCellView * __nonnull)getCellView:(UITableView * __nonnull)tableView index:(NSIndexPath * __nonnull)indexPath;
  10. @end
  11. //********************************************************************************
  12. //*自定义类 UITableViewCell 实现
  13. //********************************************************************************
  14. @implementation MItemCellView
  15. - (instancetype __nonnull)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString * __nullable)reuseIdentifier{
  16. self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
  17. return self;
  18. }
  19. //静态工厂方法
  20. + (MItemCellView * __nonnull)getCellView:(UITableView * __nonnull)tableView index:(NSIndexPath * __nonnull)indexPath{
  21. MItemCellView *cellView=(MItemCellView *)[tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
  22. //设置cell选中状态
  23. cellView.selectionStyle=UITableViewCellSelectionStyleDefault;
  24. cellView.selectedBackgroundView=[[UIView alloc]initWithFrame:cellView.frame];
  25. cellView.selectedBackgroundView.backgroundColor=[UIColor greenColor];
  26. return cellView;
  27. }
  28. @end
  29. //********************************************************************************
  30. //*自定义类 UITableViewCell END
  31. //********************************************************************************
  1. @interface HomeController () <UITableViewDelegate,UITableViewDataSource>
  2. @property (weak, nonatomic) IBOutlet UILabel *label;
  3. @property (weak, nonatomic) IBOutlet UITableView *tabview;
  4. @property NSMutableArray *dataList;
  5. @end
  6. @implementation HomeController
  7. @synthesize label;
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10. [self initLabel];
  11. //tableView init start
  12. self.dataList=[[NSMutableArray alloc]initWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",
  13. @"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil];
  14. self.tabview.delegate=self;//设置代理
  15. self.tabview.dataSource=self;//设置数据源
  16. self.tabview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;//设置间隔线style
  17. self.tabview.separatorColor=[UIColor blackColor];//设置间隔线颜色
  18. [self.tabview registerNib:[UINib nibWithNibName:@"MItemCellView" bundle:nil] forCellReuseIdentifier:cellID];//注册 cellView xib 文件
  19. //tableView init end
  20. }
  21. #pragma mark UITableViewDataSource
  22. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  23. return self.dataList.count;
  24. }
  25. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  26. return 100+1;//+1代表包含间隔线高度
  27. }
  28. //自定义的cellView
  29. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  30. MItemCellView *cellView=[MItemCellView getCellView:tableView index:indexPath];
  31. //others
  32. cellView.titleTV.text=[NSString stringWithFormat:@"%i - title:%@",indexPath.row,[self.dataList objectAtIndex:indexPath.row]];
  33. cellView.descTV.text=[NSString stringWithFormat:@"%i - desc :%@",indexPath.row,[self.dataList objectAtIndex:indexPath.row]];
  34. cellView.titleTV.enabled=false;
  35. cellView.descTV.enabled=false;
  36. return cellView;
  37. }
  38. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  39. //执行item点击事件
  40. [self performSegueWithIdentifier:@"toToCollection" sender:self];
  41. }






让过去成就未来.
原文地址:https://www.cnblogs.com/mkr127/p/5490713.html