在UITableView顶部制作简单的UISegmentControl实例方法

在UITableView顶部制作简单的UISegmentControl实例方法

在UITableView顶部制作简单的UISegmentControl实例方法

// http://www.tuicool.com/articles/yUfURj 使用方法

如上图: 分两步:

一、创建320长度背景,高不超过40最好

  1. //segment背景图
  2. UIImageView *segBgImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"segmented_bg.png"]];
  3. [segBgImg setUserInteractionEnabled:YES];
  4. [segBgImg setFrame:CGRectMake(0, 44, 320, 38)];
  5. //segment实例对象及初始化
  6. UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:nil];
  7. segment.selectedSegmentIndex = 0;
  8. [segment insertSegmentWithImage:[UIImage imageNamed:@"segmented_price_all_selected.png"] atIndex:0 animated:NO];
  9. [segment insertSegmentWithImage:[UIImage imageNamed:@"segmented_price_free.png"] atIndex:1 animated:NO];
  10. segment.frame = CGRectMake(10, 5, 184, 28);
  11. [segment setMomentary:YES];
  12. [segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
  13. [segBgImg addSubview:segment];
  14. [segment release];
  15. [self.view addSubview:segBgImg];
  16. [segBgImg release];

二、segmentClick相应事件中选中当前焦点

  1. //选择时触发
  2. - (void)segmentClick:(UISegmentedControl *)segment
  3. {
  4. NSInteger index = segment.selectedSegmentIndex;
  5. switch (index) {
  6. case 0: //全部
  7. [segment setImage:[UIImage imageNamed:@"segmented_price_all_selected.png"] forSegmentAtIndex:0];
  8. [segment setImage:[UIImage imageNamed:@"segmented_price_free.png"] forSegmentAtIndex:1];
  9. break;
  10. case 1: //免费
  11. [segment setImage:[UIImage imageNamed:@"segmented_price_all.png"] forSegmentAtIndex:0];
  12. [segment setImage:[UIImage imageNamed:@"segmented_price_free_selected.png"] forSegmentAtIndex:1];
  13. break;
  14. }
  15. }
  1. - (void)viewDidLoad {
  2. NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
  3. //初始化UISegmentedControl
  4. UISegmentedControl * segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
  5. segmentedControl.frame = CGRectMake(60.0, 10.0, 200.0, 50.0);
  6. segmentedControl.segmentedControlStyle = 2;
  7. [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
  8. [segmentedControl setImage:[UIImage imageNamed:@"lan.png"] forSegmentAtIndex:3];//设置指定索引的图片
  9. [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片
  10. [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目
  11. [segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项
  12. [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度
  13. [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];//设置选项中图片等的左上角的位置
  14. //获取指定索引选项的图片imageForSegmentAtIndex:
  15. UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
  16. imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);
  17. //获取指定索引选项的标题titleForSegmentAtIndex
  18. UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];
  19. titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
  20. //获取总选项数segmentedControl.numberOfSegments
  21. UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];
  22. numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
  23. //获取指定索引选项的宽度widthForSegmentAtIndex:
  24. UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];
  25. widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];
  26. segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引
  27. segmentedControl.tintColor = [UIColor redColor];
  28. segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
  29. segmentedControl.momentary = YES;//设置在点击后是否恢复原样
  30. [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
  31. BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
  32. NSLog(@"%d",enableFlag);
  33. }

注: 多个segment切换时用for循环处理一下即可

上一篇:Domain Driven Design and Development In Practice--转载


下一篇:git 删除本地分支、远程分支、本地回滚、远程回滚