溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

ios基于UITableViewController實(shí)現(xiàn)列表

發(fā)布時(shí)間:2020-09-18 05:33:25 來(lái)源:腳本之家 閱讀:140 作者:chenzheng8975 欄目:移動(dòng)開(kāi)發(fā)

實(shí)現(xiàn)效果圖如下:

ios基于UITableViewController實(shí)現(xiàn)列表

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 = @"四川青川縣今晨發(fā)生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將可分屏多任務(wù)"; 
  n3.count = 3278; 
  n3.imageName = @"hqg"; 
   
  News *n4 = [[News alloc]init]; 
  n4.title = @"JAVA語(yǔ)言估計(jì)下月進(jìn)入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

ios基于UITableViewController實(shí)現(xiàn)列表

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 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI