欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios基于UITableViewController实现列表

程序员文章站 2023-12-21 08:43:28
实现效果图如下: news.h #import @interface news...

实现效果图如下:

ios基于UITableViewController实现列表

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

ios基于UITableViewController实现列表

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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: