HomeTableViewCellTopic.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // HomeTableViewCellTopic.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 7/21/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "HomeTableViewCellTopic.h"
  9. #import "CategoryCellSmall.h"
  10. #import "DetailViewController.h"
  11. #import "FileCache.h"
  12. @implementation HomeTableViewCellTopic
  13. @synthesize itemdelegate;
  14. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  15. {
  16. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  17. if (self) {
  18. // Initialization code
  19. }
  20. return self;
  21. }
  22. //- (void)awakeFromNib {
  23. // // Initialization code
  24. //}
  25. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  26. [super setSelected:selected animated:animated];
  27. // Configure the view for the selected state
  28. }
  29. #pragma mark -- UICollectionViewDataSource
  30. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  31. {
  32. // 每个Section的item个数
  33. int count =[[self.related_data valueForKey:@"count"] intValue];
  34. return count;
  35. }
  36. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  37. {
  38. return 1;
  39. }
  40. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  41. {
  42. static NSString * CellIdentifier = @"RelatedModelCell";
  43. CategoryCellSmall * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  44. NSDictionary * item_json =[self.related_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  45. NSString* img_url =[item_json valueForKey:@"picture_path"];
  46. NSString* description =[item_json valueForKey:@"fash_name"];
  47. // NSString* old_price =[item_json valueForKey:@"old_price"];
  48. // NSString* price =[item_json valueForKey:@"price"];
  49. cell.cellDescription.text = description;
  50. // cell.oldPrice.text = old_price;
  51. // cell.Price.text = price;
  52. cell.cellImageView.image = [UIImage imageNamed:@"loading_s"];
  53. NSString* file_name=[img_url lastPathComponent];
  54. NSData* img_data=[FileCache load_cached_img:file_name loadFrom:img_url];
  55. if(img_data!=nil)
  56. {
  57. UIImage * img =[UIImage imageWithData:img_data];
  58. cell.cellImageView.image = img;
  59. }
  60. else
  61. {
  62. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  63. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. if(downloadimg_data!=nil)
  66. {
  67. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  68. UIImage * img =[UIImage imageWithData:downloadimg_data];
  69. cell.cellImageView.image = img;
  70. }
  71. else
  72. cell.cellImageView.image = [UIImage imageNamed:@"notfound_s"];
  73. });
  74. });
  75. }
  76. return cell;
  77. }
  78. #pragma mark --UICollectionViewDelegateFlowLayout
  79. ////定义每个UICollectionView 的大小
  80. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  81. //{
  82. // return CGSizeMake(96, 100);
  83. //}
  84. //定义每个UICollectionView 的 margin
  85. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  86. {
  87. return UIEdgeInsetsMake(10, 10, 10, 10);
  88. }
  89. #pragma mark --UICollectionViewDelegate
  90. //UICollectionView被选中时调用的方法
  91. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. NSDictionary* json=[self.related_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  94. NSString* product_id = [json valueForKey:@"product_id"];
  95. NSString* category = [json valueForKey:@"category"];
  96. [itemdelegate TopicItemClicked : product_id category:category];
  97. }
  98. @end