服务器之家

服务器之家 > 正文

ios基于UITableViewController实现列表

时间:2021-03-03 15:54     来源/作者:chenzheng8975

实现效果图如下:

ios基于UITableViewController实现列表

news.h

?
1
2
3
4
5
6
7
8
9
10
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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

?
1
2
3
4
5
6
7
8
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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

?
1
2
3
4
5
#import <uikit/uikit.h>
 
@interface newstableviewcontroller : uitableviewcontroller
@property (nonatomic, strong) nsarray *news;
@end

newstableviewcontroller.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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

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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部