You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
22 KiB

  1. //
  2. // MLXYViewController.m
  3. // HC
  4. //
  5. // Created by huilinLi on 2025/11/26.
  6. //
  7. #import "MLXYViewController.h"
  8. #import "ChartViewController.h"
  9. @interface MLXYViewController () <UITableViewDataSource, UITableViewDelegate>
  10. @property (nonatomic, strong) UIButton *mlxyButton;
  11. // 大盘指数
  12. @property (nonatomic, strong) UILabel *marketTitleLabel;
  13. @property (nonatomic, strong) UIView *marketView;
  14. @property (nonatomic, strong) UILabel *marketLine1;
  15. @property (nonatomic, strong) UILabel *marketLine2;
  16. @property (nonatomic, strong) UILabel *marketLine3;
  17. // 板块
  18. @property (nonatomic, strong) UILabel *sectorTitleLabel;
  19. @property (nonatomic, strong) UIButton *sectorBtn;
  20. // 三个板块卡片(分别配置)
  21. @property (nonatomic, strong) UIView *sectorCard1;
  22. @property (nonatomic, strong) UIView *sectorCard2;
  23. @property (nonatomic, strong) UIView *sectorCard3;
  24. // 股票
  25. @property (nonatomic, strong) UIView *stockContainer;
  26. @property (nonatomic, strong) UITableView *stockTableView;
  27. // 股票表头
  28. @property (nonatomic, strong) UIView *stockHeaderView;
  29. // 按钮配置
  30. @property (nonatomic, strong) UIButtonConfiguration *buttonConfig;
  31. // 板块数据配置
  32. @property (nonatomic, strong) NSArray *sectorDataConfig;
  33. @end
  34. @implementation MLXYViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.view.backgroundColor = [UIColor blackColor];
  38. [self setupButtonConfig];
  39. [self setupSectorDataConfig];
  40. [self setupSubviews];
  41. [self setupConstraints];
  42. [self marketViewClick];
  43. }
  44. -(void) marketViewClick{
  45. _marketView.userInteractionEnabled = YES;
  46. UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(marketViewPush)];
  47. [_marketView addGestureRecognizer:click];
  48. }
  49. -(void) marketViewPush{
  50. ChartViewController *chartViewController = [[ChartViewController alloc] init];
  51. [self.navigationController pushViewController:chartViewController animated:YES];
  52. }
  53. - (void)setupButtonConfig {
  54. UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
  55. config.title = @"更多";
  56. config.attributedTitle = [[NSAttributedString alloc] initWithString:@"更多" attributes:@{
  57. NSFontAttributeName: [UIFont systemFontOfSize:14],
  58. NSForegroundColorAttributeName: [UIColor lightGrayColor]
  59. }];
  60. UIImage *image = [[UIImage systemImageNamed:@"chevron.right"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  61. config.image = image;
  62. config.imagePlacement = NSDirectionalRectEdgeTrailing; // 图片在文字右侧
  63. config.imagePadding = 1;
  64. config.baseForegroundColor = [UIColor lightGrayColor];
  65. _buttonConfig = config;
  66. }
  67. - (void)setupSectorDataConfig {
  68. _sectorDataConfig = @[
  69. @{@"name":@"Health", @"value1":@"1099.683", @"value2":@"27.236",@"value3":@"2.270%"},
  70. @{@"name":@"Others", @"value1":@"2083.783", @"value2":@"0.000",@"value3":@"0.000%"},
  71. @{@"name":@"HLKLI", @"value1":@"987.895", @"value2":@"-1.854",@"value3":@"-0.230%"}
  72. ];
  73. }
  74. - (void)setupSubviews {
  75. // 马来西亚按钮
  76. _mlxyButton = [UIButton buttonWithType:UIButtonTypeSystem];
  77. [_mlxyButton setTitle:@"马来西亚" forState:UIControlStateNormal];
  78. [_mlxyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  79. [_mlxyButton setBackgroundColor:[UIColor blueColor]];
  80. _mlxyButton.layer.cornerRadius = 15;
  81. _mlxyButton.titleLabel.font = [UIFont systemFontOfSize:14];
  82. _mlxyButton.translatesAutoresizingMaskIntoConstraints = NO;
  83. [self.view addSubview:_mlxyButton];
  84. // 大盘指数
  85. _marketTitleLabel = [[UILabel alloc] init];
  86. _marketTitleLabel.text = @"大盘指数";
  87. _marketTitleLabel.textColor = [UIColor whiteColor];
  88. _marketTitleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  89. _marketTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  90. [self.view addSubview:_marketTitleLabel];
  91. // 绿色卡片
  92. _marketView = [[UIView alloc] init];
  93. _marketView.backgroundColor = [UIColor greenColor];
  94. _marketView.layer.cornerRadius = 8;
  95. _marketView.translatesAutoresizingMaskIntoConstraints = NO;
  96. [self.view addSubview:_marketView];
  97. _marketLine1 = [[UILabel alloc] init];
  98. _marketLine1.text = @"富时马来西亚KLCI";
  99. _marketLine1.textColor = [UIColor whiteColor];
  100. _marketLine1.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
  101. _marketLine1.textAlignment = NSTextAlignmentCenter;
  102. _marketLine1.translatesAutoresizingMaskIntoConstraints = NO;
  103. [_marketView addSubview:_marketLine1];
  104. _marketLine2 = [[UILabel alloc] init];
  105. _marketLine2.text = @"1624.500";
  106. _marketLine2.textColor = [UIColor whiteColor];
  107. _marketLine2.font = [UIFont boldSystemFontOfSize:17];
  108. _marketLine2.textAlignment = NSTextAlignmentCenter;
  109. _marketLine2.translatesAutoresizingMaskIntoConstraints = NO;
  110. [_marketView addSubview:_marketLine2];
  111. _marketLine3 = [[UILabel alloc] init];
  112. _marketLine3.text = @"12.760 0.792%";
  113. _marketLine3.textColor = [UIColor whiteColor];
  114. _marketLine3.font = [UIFont boldSystemFontOfSize:12];
  115. _marketLine3.textAlignment = NSTextAlignmentCenter;
  116. _marketLine3.translatesAutoresizingMaskIntoConstraints = NO;
  117. [_marketView addSubview:_marketLine3];
  118. // 板块
  119. _sectorTitleLabel = [[UILabel alloc] init];
  120. _sectorTitleLabel.text = @"板块";
  121. _sectorTitleLabel.textColor = [UIColor whiteColor];
  122. _sectorTitleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  123. _sectorTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  124. [self.view addSubview:_sectorTitleLabel];
  125. _sectorBtn = [UIButton buttonWithConfiguration:self.buttonConfig primaryAction:nil];
  126. _sectorBtn.translatesAutoresizingMaskIntoConstraints = NO;
  127. [self.view addSubview:_sectorBtn];
  128. _sectorCard1 = [self createSectorCardWithData:self.sectorDataConfig[0]];
  129. [self.view addSubview:_sectorCard1];
  130. _sectorCard2 = [self createSectorCardWithData:self.sectorDataConfig[1]];
  131. [self.view addSubview:_sectorCard2];
  132. _sectorCard3 = [self createSectorCardWithData:self.sectorDataConfig[2]];
  133. [self.view addSubview:_sectorCard3];
  134. // 股票容器
  135. _stockContainer = [[UIView alloc] init];
  136. _stockContainer.translatesAutoresizingMaskIntoConstraints = NO;
  137. [self.view addSubview:_stockContainer];
  138. UILabel *stockTitleLabel = [[UILabel alloc] init];
  139. stockTitleLabel.text = @"股票";
  140. stockTitleLabel.textColor = [UIColor whiteColor];
  141. stockTitleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  142. stockTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  143. [_stockContainer addSubview:stockTitleLabel];
  144. UIButton *stockBtn = [UIButton buttonWithConfiguration:self.buttonConfig primaryAction:nil];
  145. stockBtn.translatesAutoresizingMaskIntoConstraints = NO;
  146. [_stockContainer addSubview:stockBtn];
  147. // 股票列表表头
  148. _stockHeaderView = [[UIView alloc] init];
  149. _stockHeaderView.backgroundColor = [UIColor blackColor];
  150. _stockHeaderView.translatesAutoresizingMaskIntoConstraints = NO;
  151. [_stockContainer addSubview:_stockHeaderView];
  152. NSArray *headerTitles = @[@"名称", @"最新", @"涨幅"];
  153. for (NSInteger i = 0; i < headerTitles.count; i++) {
  154. UILabel *headerLabel = [[UILabel alloc] init];
  155. headerLabel.text = headerTitles[i];
  156. headerLabel.textColor = [UIColor lightGrayColor];
  157. headerLabel.font = [UIFont systemFontOfSize:12];
  158. headerLabel.textAlignment = (i == 0) ? NSTextAlignmentLeft : NSTextAlignmentRight;
  159. headerLabel.translatesAutoresizingMaskIntoConstraints = NO;
  160. [_stockHeaderView addSubview:headerLabel];
  161. if (i == 0) {
  162. [headerLabel.leadingAnchor constraintEqualToAnchor:_stockHeaderView.leadingAnchor constant:15].active = YES;
  163. } else if (i == 1) {
  164. [headerLabel.widthAnchor constraintEqualToConstant:60].active = YES;
  165. [headerLabel.trailingAnchor constraintEqualToAnchor:_stockHeaderView.trailingAnchor constant:-110].active = YES;
  166. } else {
  167. [headerLabel.widthAnchor constraintEqualToConstant:80].active = YES;
  168. [headerLabel.trailingAnchor constraintEqualToAnchor:_stockHeaderView.trailingAnchor constant:-30].active = YES;
  169. }
  170. [headerLabel.centerYAnchor constraintEqualToAnchor:_stockHeaderView.centerYAnchor].active = YES;
  171. }
  172. _stockTableView = [[UITableView alloc] init];
  173. _stockTableView.backgroundColor = [UIColor blackColor];
  174. _stockTableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 隐藏单元格之间的分隔线
  175. //SingleLine 默认,1px的分割线
  176. _stockTableView.delegate = self;
  177. _stockTableView.dataSource = self;
  178. [_stockTableView registerClass:[UITableViewCell class]// 单元格复用类
  179. forCellReuseIdentifier:@"StockCell"];// 复用标识
  180. _stockTableView.translatesAutoresizingMaskIntoConstraints = NO;
  181. [_stockContainer addSubview:_stockTableView];
  182. }
  183. - (UIView *)createSectorCardWithData:(NSDictionary *)data {
  184. UIView *card = [[UIView alloc] init];
  185. card.backgroundColor = [UIColor darkGrayColor];
  186. card.layer.cornerRadius = 8;
  187. card.translatesAutoresizingMaskIntoConstraints = NO;
  188. // 名称
  189. UILabel *nameLabel = [[UILabel alloc] init];
  190. nameLabel.text = data[@"name"];
  191. nameLabel.textColor = [UIColor whiteColor];
  192. nameLabel.font = [UIFont systemFontOfSize:14];
  193. nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
  194. [card addSubview:nameLabel];
  195. // 数值
  196. UILabel *value1Label = [[UILabel alloc] init];
  197. value1Label.text = data[@"value1"];
  198. value1Label.textColor = [UIColor greenColor];
  199. value1Label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
  200. value1Label.translatesAutoresizingMaskIntoConstraints = NO;
  201. [card addSubview:value1Label];
  202. // 涨幅
  203. UILabel *value2Label = [[UILabel alloc] init];
  204. value2Label.text = data[@"value2"];
  205. value2Label.textColor = [UIColor greenColor];
  206. value2Label.font = [UIFont systemFontOfSize:12];
  207. value2Label.translatesAutoresizingMaskIntoConstraints = NO;
  208. [card addSubview:value2Label];
  209. UILabel *value3Label = [[UILabel alloc] init];
  210. value3Label.text = data[@"value3"];
  211. value3Label.textColor = [UIColor greenColor];
  212. value3Label.font = [UIFont systemFontOfSize:12];
  213. value3Label.translatesAutoresizingMaskIntoConstraints = NO;
  214. [card addSubview:value3Label];
  215. [NSLayoutConstraint activateConstraints:@[
  216. [nameLabel.topAnchor constraintEqualToAnchor:card.topAnchor constant:7],
  217. [nameLabel.leadingAnchor constraintEqualToAnchor:card.leadingAnchor constant:10],
  218. [value1Label.topAnchor constraintEqualToAnchor:nameLabel.bottomAnchor constant:10],
  219. [value1Label.leadingAnchor constraintEqualToAnchor:card.leadingAnchor constant:10],
  220. [value2Label.topAnchor constraintEqualToAnchor:value1Label.bottomAnchor constant:7],
  221. [value2Label.leadingAnchor constraintEqualToAnchor:card.leadingAnchor constant:10],
  222. [value3Label.topAnchor constraintEqualToAnchor:value2Label.bottomAnchor constant:6],
  223. [value3Label.leadingAnchor constraintEqualToAnchor:card.leadingAnchor constant:10]
  224. ]];
  225. return card;
  226. }
  227. - (void)setupConstraints {
  228. // 马来西亚按钮
  229. NSLayoutConstraint *btnTop = [_mlxyButton.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor];
  230. NSLayoutConstraint *btnLeft = [_mlxyButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:15];
  231. NSLayoutConstraint *btnWidth = [_mlxyButton.widthAnchor constraintEqualToConstant:90];
  232. NSLayoutConstraint *btnHeight = [_mlxyButton.heightAnchor constraintEqualToConstant:30];
  233. [NSLayoutConstraint activateConstraints:@[btnTop, btnLeft, btnWidth, btnHeight]];
  234. // 大盘
  235. NSLayoutConstraint *marketTitleTop = [_marketTitleLabel.topAnchor constraintEqualToAnchor:_mlxyButton.bottomAnchor constant:10];
  236. NSLayoutConstraint *marketTitleLeft = [_marketTitleLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:15];
  237. [NSLayoutConstraint activateConstraints:@[marketTitleTop, marketTitleLeft]];
  238. NSLayoutConstraint *marketCardTop = [_marketView.topAnchor constraintEqualToAnchor:_marketTitleLabel.bottomAnchor constant:10];
  239. NSLayoutConstraint *marketCardLeft = [_marketView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:15];
  240. NSLayoutConstraint *marketCardWidth = [_marketView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:0.3];
  241. NSLayoutConstraint *marketCardHeight = [_marketView.heightAnchor constraintEqualToConstant:100];
  242. [NSLayoutConstraint activateConstraints:@[marketCardTop, marketCardLeft, marketCardWidth, marketCardHeight]];
  243. NSLayoutConstraint *line1Top = [_marketLine1.topAnchor constraintEqualToAnchor:_marketView.topAnchor constant:10];
  244. NSLayoutConstraint *line1CenterX = [_marketLine1.centerXAnchor constraintEqualToAnchor:_marketView.centerXAnchor];
  245. NSLayoutConstraint *line2Top = [_marketLine2.topAnchor constraintEqualToAnchor:_marketLine1.bottomAnchor constant:15];
  246. NSLayoutConstraint *line2CenterX = [_marketLine2.centerXAnchor constraintEqualToAnchor:_marketView.centerXAnchor];
  247. NSLayoutConstraint *line3Top = [_marketLine3.topAnchor constraintEqualToAnchor:_marketLine2.bottomAnchor constant:12];
  248. NSLayoutConstraint *line3CenterX = [_marketLine3.centerXAnchor constraintEqualToAnchor:_marketView.centerXAnchor];
  249. [NSLayoutConstraint activateConstraints:@[line1Top, line1CenterX, line2Top, line2CenterX, line3Top, line3CenterX]];
  250. // 板块
  251. NSLayoutConstraint *sectorTitleTop = [_sectorTitleLabel.topAnchor constraintEqualToAnchor:_marketView.bottomAnchor constant:20];
  252. NSLayoutConstraint *sectorTitleLeft = [_sectorTitleLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:15];
  253. [NSLayoutConstraint activateConstraints:@[sectorTitleTop, sectorTitleLeft]];
  254. NSLayoutConstraint *sectorBtnTop = [_sectorBtn.centerYAnchor constraintEqualToAnchor:_sectorTitleLabel.centerYAnchor];
  255. NSLayoutConstraint *sectorBtnRight = [_sectorBtn.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-15];
  256. [NSLayoutConstraint activateConstraints:@[sectorBtnTop, sectorBtnRight]];
  257. NSLayoutConstraint *card1Top = [_sectorCard1.topAnchor constraintEqualToAnchor:_sectorTitleLabel.bottomAnchor constant:10];
  258. NSLayoutConstraint *card1Left = [_sectorCard1.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:8];
  259. NSLayoutConstraint *card1Width = [_sectorCard1.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:0.3];
  260. NSLayoutConstraint *card1Height = [_sectorCard1.heightAnchor constraintEqualToConstant:100];
  261. [NSLayoutConstraint activateConstraints:@[card1Top, card1Left, card1Width, card1Height]];
  262. NSLayoutConstraint *card2Top = [_sectorCard2.topAnchor constraintEqualToAnchor:_sectorCard1.topAnchor];
  263. NSLayoutConstraint *card2Left = [_sectorCard2.leadingAnchor constraintEqualToAnchor:_sectorCard1.trailingAnchor constant:8];
  264. NSLayoutConstraint *card2Width = [_sectorCard2.widthAnchor constraintEqualToAnchor:_sectorCard1.widthAnchor];
  265. NSLayoutConstraint *card2Height = [_sectorCard2.heightAnchor constraintEqualToAnchor:_sectorCard1.heightAnchor];
  266. [NSLayoutConstraint activateConstraints:@[card2Top, card2Left, card2Width, card2Height]];
  267. NSLayoutConstraint *card3Top = [_sectorCard3.topAnchor constraintEqualToAnchor:_sectorCard1.topAnchor];
  268. NSLayoutConstraint *card3Left = [_sectorCard3.leadingAnchor constraintEqualToAnchor:_sectorCard2.trailingAnchor constant:8];
  269. NSLayoutConstraint *card3Right = [_sectorCard3.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16];
  270. // *card3Width = [_sectorCard3.widthAnchor constraintEqualToAnchor:_sectorCard1.widthAnchor];都行
  271. NSLayoutConstraint *card3Height = [_sectorCard3.heightAnchor constraintEqualToAnchor:_sectorCard1.heightAnchor];
  272. [NSLayoutConstraint activateConstraints:@[card3Top, card3Left, card3Right, card3Height]];
  273. // 股票容器
  274. NSLayoutConstraint *stockTop = [_stockContainer.topAnchor constraintEqualToAnchor:_sectorCard1.bottomAnchor constant:20];
  275. NSLayoutConstraint *stockLeft = [_stockContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor];
  276. NSLayoutConstraint *stockRight = [_stockContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor];
  277. NSLayoutConstraint *stockBottom = [_stockContainer.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor];
  278. [NSLayoutConstraint activateConstraints:@[stockTop, stockLeft, stockRight, stockBottom]];
  279. UILabel *stockTitleLabel = _stockContainer.subviews[0];
  280. UIButton *stockBtn = _stockContainer.subviews[1];
  281. NSLayoutConstraint *stockTitleTop = [stockTitleLabel.topAnchor constraintEqualToAnchor:_stockContainer.topAnchor constant:15];
  282. NSLayoutConstraint *stockTitleLeft = [stockTitleLabel.leadingAnchor constraintEqualToAnchor:_stockContainer.leadingAnchor constant:15];
  283. NSLayoutConstraint *stockBtnTop = [stockBtn.centerYAnchor constraintEqualToAnchor:stockTitleLabel.centerYAnchor];
  284. NSLayoutConstraint *stockBtnRight = [stockBtn.trailingAnchor constraintEqualToAnchor:_stockContainer.trailingAnchor constant:-15];
  285. [NSLayoutConstraint activateConstraints:@[stockTitleTop, stockTitleLeft, stockBtnTop, stockBtnRight]];
  286. // 股票表头
  287. NSLayoutConstraint *headerTop = [_stockHeaderView.topAnchor constraintEqualToAnchor:stockTitleLabel.bottomAnchor constant:10];
  288. NSLayoutConstraint *headerLeft = [_stockHeaderView.leadingAnchor constraintEqualToAnchor:_stockContainer.leadingAnchor];
  289. NSLayoutConstraint *headerRight = [_stockHeaderView.trailingAnchor constraintEqualToAnchor:_stockContainer.trailingAnchor];
  290. NSLayoutConstraint *headerHeight = [_stockHeaderView.heightAnchor constraintEqualToConstant:30];
  291. [NSLayoutConstraint activateConstraints:@[headerTop, headerLeft, headerRight, headerHeight]];
  292. // 股票列表
  293. NSLayoutConstraint *tableTop = [_stockTableView.topAnchor constraintEqualToAnchor:_stockHeaderView.bottomAnchor];
  294. NSLayoutConstraint *tableLeft = [_stockTableView.leadingAnchor constraintEqualToAnchor:_stockContainer.leadingAnchor];
  295. NSLayoutConstraint *tableRight = [_stockTableView.trailingAnchor constraintEqualToAnchor:_stockContainer.trailingAnchor];
  296. NSLayoutConstraint *tableBottom = [_stockTableView.bottomAnchor constraintEqualToAnchor:_stockContainer.bottomAnchor];
  297. [NSLayoutConstraint activateConstraints:@[tableTop, tableLeft, tableRight, tableBottom]];
  298. }
  299. #pragma mark - UITableView
  300. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  301. return 4;
  302. }
  303. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  304. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StockCell" forIndexPath:indexPath];
  305. cell.backgroundColor = [UIColor blackColor];
  306. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  307. // 清空复用内容
  308. [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  309. NSArray *stocks = @[
  310. @{@"name":@"PEGASUS HEIGHTS BERHAD", @"code":@"4464", @"price":@"0.010", @"value2":@"100.000%"},
  311. @{@"name":@"SMTRACK BERHAD", @"code":@"0169", @"price":@"0.010", @"value2":@"100.000%"},
  312. @{@"name":@"TXCD BERHAD - ICPS 2020/2030", @"code":@"7145PA", @"price":@"0.020", @"value2":@"33.333%"},
  313. @{@"name":@"DNONCE TECHNOLOGY BHD", @"code":@"7114", @"price":@"0.040", @"value2":@"33.333%"}
  314. ];
  315. NSDictionary *stock = stocks[indexPath.row];
  316. // 名称
  317. UILabel *nameLabel = [[UILabel alloc] init];
  318. nameLabel.text = stock[@"name"];
  319. nameLabel.textColor = [UIColor whiteColor];
  320. nameLabel.font = [UIFont systemFontOfSize:14];
  321. nameLabel.numberOfLines = 1;
  322. nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  323. nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
  324. [cell.contentView addSubview:nameLabel];
  325. // 代码
  326. UILabel *codeLabel = [[UILabel alloc] init];
  327. codeLabel.text = stock[@"code"];
  328. codeLabel.textColor = [UIColor lightGrayColor];
  329. codeLabel.font = [UIFont systemFontOfSize:12];
  330. codeLabel.translatesAutoresizingMaskIntoConstraints = NO;
  331. [cell.contentView addSubview:codeLabel];
  332. // 价格
  333. UILabel *priceLabel = [[UILabel alloc] init];
  334. priceLabel.text = stock[@"price"];
  335. priceLabel.textColor = [UIColor greenColor];
  336. priceLabel.font = [UIFont systemFontOfSize:14];
  337. priceLabel.textAlignment = NSTextAlignmentRight;
  338. priceLabel.translatesAutoresizingMaskIntoConstraints = NO;
  339. [cell.contentView addSubview:priceLabel];
  340. // 涨幅
  341. UILabel *value2Label = [[UILabel alloc] init];
  342. value2Label.text = stock[@"value2"];
  343. value2Label.textColor = [UIColor greenColor];
  344. value2Label.font = [UIFont systemFontOfSize:14];
  345. value2Label.textAlignment = NSTextAlignmentRight;
  346. value2Label.translatesAutoresizingMaskIntoConstraints = NO;
  347. [cell.contentView addSubview:value2Label];
  348. // 约束
  349. [NSLayoutConstraint activateConstraints:@[
  350. [nameLabel.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:15],
  351. [nameLabel.topAnchor constraintEqualToAnchor:cell.contentView.topAnchor constant:10],
  352. [nameLabel.trailingAnchor constraintLessThanOrEqualToAnchor:priceLabel.leadingAnchor constant:-10],
  353. [codeLabel.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:15],
  354. [codeLabel.topAnchor constraintEqualToAnchor:nameLabel.bottomAnchor constant:5],
  355. [priceLabel.widthAnchor constraintEqualToConstant:60],
  356. [priceLabel.trailingAnchor constraintEqualToAnchor:value2Label.leadingAnchor constant:-10],
  357. [priceLabel.centerYAnchor constraintEqualToAnchor:cell.contentView.centerYAnchor],
  358. [value2Label.widthAnchor constraintEqualToConstant:80],
  359. [value2Label.trailingAnchor constraintEqualToAnchor:cell.contentView.trailingAnchor constant:-15],
  360. [value2Label.centerYAnchor constraintEqualToAnchor:cell.contentView.centerYAnchor]
  361. ]];
  362. return cell;
  363. }
  364. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  365. return 60;
  366. }
  367. @end