实现效果图如下:
新闻。h
# import & lt;基?Foundation.h> @ interface新闻:NSObject @ property(原子、强)NSString *标题; @ property(原子)了NSUInteger计数; @ property(原子、强)NSString * imageName; *)+ (NSArray demoData; @end & lt;/strong> >之前新闻。m
#进口“News.h” @ implementation新闻 + (NSArray *) demoData { 新闻* n1=[[新闻alloc] init); n1。标题=@“四川青川县今晨发生4.8地震”; n1。数=2175; n1。imageName=@“hqg”; 新闻* n2=[[新闻alloc] init); n2。title=@ 3名夺刀少年遭多所高校\“哄抢\ "; n2。数=987; n2。imageName=@“hqg”; 新闻* n3=[[新闻alloc] init); n3。标题=@“代码显示Eclipse将可分屏多任务”; n3。数=3278; n3。imageName=@“hqg”; 新闻*陶瓷=[[新闻alloc] init); 陶瓷。title=@“JAVA语言估计下月进入TIOBE前20名”; 陶瓷。数=1462; 陶瓷。imageName=@“hqg”; 回复@ (n1、n2, n3,陶瓷); }@end >之前NewsCell。h
# import & lt; UIKit/UIKit.h> @ interface NewsCell: UITableViewCell @ property(弱,原子)IBOutlet UIImageView * newsImageView; @ property(弱,原子)IBOutlet UILabel * titleLabel; @ property(弱,原子)IBOutlet UILabel * countLabel; @end >之前NewsCell。m
#进口“NewsCell.h” @ implementation NewsCell - (void) awakeFromNib {//初始化代码 } - (void) setSelected: (BOOL)选择动画:动画(BOOL) { (超级setSelected:选择动画:动画);//为选定的配置视图状态 } @end >之前NewsCell.xib
NewsTableViewController。h
# import & lt; UIKit/UIKit.h> @ interface NewsTableViewController: UITableViewController @ property(原子、强)NSArray *新闻; @end >之前NewsTableViewController。m
#进口“NewsTableViewController.h” #进口“News.h” #进口“NewsCell.h” @ interface NewsTableViewController () @end @ implementation NewsTableViewController 静态NSString * cellIdentifier=@“MyNewsCell”; - (void) viewDidLoad { (超级viewDidLoad); 自我。新闻=[新闻demoData]; 自我。标题=@“腾讯新闻”; UINib * nib=[UINib nibWithNibName: @“NewsCell”包:nil); (自我。tableView registerNib: nib forCellReuseIdentifier: cellIdentifier]; } - (void) didReceiveMemoryWarning { (超级didReceiveMemoryWarning);//处理任何资源都可以被重新创建。 } # pragma马克-表视图数据来源 ——(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView { 返回1; } ——(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection:也(NSInteger)节{ 返回self.news.count; } ——(CGFloat) tableView: (UITableView *)视图 indexPath heightForRowAtIndexPath:(NSIndexPath *) { 返回86; } ——(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 新闻*=self.news (indexpath。row); NewsCell *细胞=[tableView dequeueReusableCellWithIdentifier cellIdentifier):; cell.titleLabel。文本=news.title; cell.countLabel。文本=[NSString stringWithFormat: @ % ld, news.count); cell.newsImageView。形象=[界面图像imageNamed news.imageName):; 返回单元; } @end >之前以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
ios基于UITableViewController实现列表