NewsItemTableViewCell.h
#import <UIKit/UIKit.h>
@interface NewsItemTableViewCell : UITableViewCell
@property (nonatomic, strong) UIImageView *imageCover;
@property (nonatomic, strong) UILabel *labelTitle;
-(void)setImage:(NSString *)img setTitle:(NSString *)title;
@end
NewsItemTableViewCell.m
#import "NewsItemTableViewCell.h"
@implementation NewsItemTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
//创建imageView添加到cell中
// UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beauty.jpg"]];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(10, 10, 150, 80);
[self setImageCover:imageView];
[self addSubview:imageView];
//创建labelTitle添加到cell中
UILabel *labelTitle = [[UILabel alloc]init];
labelTitle.frame=CGRectMake(170, 10, 100, 20);
[self setLabelTitle:labelTitle];
[self addSubview:labelTitle];
}
return self;
}
-(void)setImage:(NSString *)img setTitle:(NSString *)title{
[self.imageCover setImage:[UIImage imageNamed:img]];
[self.labelTitle setText:title];
}
@end
NewsListTableViewController.m
#import "NewsListTableViewController.h"
#import "NewsItemTableViewCell.h"
#import "News.h"
@interface NewsListTableViewController (){
NSMutableArray* newsArray;
}
@end
@implementation NewsListTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
newsArray=[[NSMutableArray alloc]init];
[newsArray addObject:[[News alloc]initWithPicUrl:@"beauty.jpg" title:@"青青子衿 悠悠我心"]];
[newsArray addObject:[[News alloc]initWithPicUrl:@"helle.jpg" title:@"但为君故 沉吟至今"]];
[newsArray addObject:[[News alloc]initWithPicUrl:@"wawa.jpg" title:@"呦呦鹿鸣 食野之萍"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
return [newsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
NewsItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
//单元格样式设置为UITableViewCellStyleDefault
cell = [[NewsItemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
News* news=[newsArray objectAtIndex:indexPath.row];
[cell setImage:[news picUrl] setTitle:[news title]];
return cell;
}
//设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat
{
//这里设置成100
return 100;
}
@end
效果
屏幕快照 2017-12-13 下午10.39.12.png
网友评论