ios基于UITableViewController实现列表

时间:2021-10-08 03:39:26

实现效果图如下:

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

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

相关文章