欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

ios基于UITableViewController实现列表

时间:2021-02-18 11:20:08|栏目:iOS代码|点击:

实现效果图如下:

News.h

#import <Foundation/Foundation.h> 
 
@interface News : NSObject 
 
@property (nonatomic, strong) NSString *title; 
@property (nonatomic) NSUInteger count; 
@property (nonatomic, strong) NSString *imageName; 
+ (NSArray *)demoData; 
@end<strong> 
</strong> 

News.m

#import "News.h" 
 
@implementation News 
+ (NSArray *)demoData 
{ 
  News *n1 = [[News alloc]init]; 
  n1.title = @"四川青川县今晨发生4.8地震"; 
  n1.count = 2175; 
  n1.imageName = @"hqg"; 
   
  News *n2 = [[News alloc]init]; 
  n2.title = @"3名夺刀少年遭多所高校\"哄抢\""; 
  n2.count = 987; 
  n2.imageName = @"hqg"; 
   
  News *n3 = [[News alloc]init]; 
  n3.title = @"代码显示Eclipse将可分屏多任务"; 
  n3.count = 3278; 
  n3.imageName = @"hqg"; 
   
  News *n4 = [[News alloc]init]; 
  n4.title = @"JAVA语言估计下月进入TIOBE前20名"; 
  n4.count = 1462; 
  n4.imageName = @"hqg"; 
  return @[n1, n2, n3, n4]; 
}@end 

NewsCell.h

#import <UIKit/UIKit.h> 
 
@interface NewsCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *newsImageView; 
@property (weak, nonatomic) IBOutlet UILabel *titleLabel; 
@property (weak, nonatomic) IBOutlet UILabel *countLabel; 
 
@end 

NewsCell.m

#import "NewsCell.h" 
 
@implementation NewsCell 
 
- (void)awakeFromNib { 
  // Initialization code 
} 
 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
  [super setSelected:selected animated:animated]; 
 
  // Configure the view for the selected state 
} 
 
@end 

NewsCell.xib

NewsTableViewController.h

#import <UIKit/UIKit.h> 
 
@interface NewsTableViewController : UITableViewController 
@property (nonatomic, strong) NSArray *news; 
@end 

NewsTableViewController.m

#import "NewsTableViewController.h" 
#import "News.h" 
#import "NewsCell.h" 
 
@interface NewsTableViewController () 
 
@end 
 
@implementation NewsTableViewController 
static NSString *cellIdentifier = @"MyNewsCell"; 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  self.news = [News demoData]; 
  self.title = @"腾讯新闻"; 
  UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:nil]; 
  [self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; 
} 
 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
 
#pragma mark - Table view data source 
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
  return 1; 
} 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
  return self.news.count; 
} 
 
-(CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
  return 86; 
} 
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
   
  News *news = self.news[indexPath.row]; 
  NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
  cell.titleLabel.text = news.title; 
  cell.countLabel.text = [NSString stringWithFormat:@"%ld", news.count]; 
  cell.newsImageView.image = [UIImage imageNamed:news.imageName]; 
  return cell; 
} 
 
@end 

上一篇:IOS图片设置毛玻璃效果

栏    目:iOS代码

下一篇:iOS archive保存图片到本地的方法

本文标题:ios基于UITableViewController实现列表

本文地址:http://www.codeinn.net/misctech/65303.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有