二十七:新闻资讯列表页
有了首页二手房的经验,新闻资讯列表显得简单的多。先看看效果吧
1、创建实体文件NewsListModel
#import <Foundation/Foundation.h> @interface NewsListModel : NSObject @property(nonatomic,assign) NSInteger NewsID; @property(nonatomic,copy) NSString *NewsTitle; @property(nonatomic,copy) NSString *NewsDesc; @property(nonatomic,copy) NSString *NewsDate; @property(nonatomic,copy) NSString *NewsClassName; @property(nonatomic,copy) NSString *NewsImage; -(instancetype)initWithDict:(NSDictionary *)dict; @end
#import "NewsListModel.h" @implementation NewsListModel -(instancetype)initWithDict:(NSDictionary *)dict { if (self=[super init]) { _NewsID = [[dict objectForKey:@"NewsID"] integerValue]; _NewsTitle = [dict objectForKey:@"NewsTitle"]; _NewsDesc = [dict objectForKey:@"NewsDesc"]; _NewsDate = [dict objectForKey:@"NewsDate"]; _NewsClassName = [dict objectForKey:@"NewsClassName"]; _NewsImage = [dict objectForKey:@"NewsImage"]; } return self; } @end
2、创建单元格原型文件NewsListCell
#import <UIKit/UIKit.h> #import "NewsListModel.h" @interface NewsListCell : UITableViewCell @property(nonatomic,strong) UIImageView *NewsImage; @property(nonatomic,strong) UILabel *NewsTitle; @property(nonatomic,strong) UILabel *NewsClassName; @property(nonatomic,strong) UILabel *NewsDate; @property(nonatomic,strong) UILabel *NewsDesc; @property(nonatomic,strong) NewsListModel *news; +(instancetype)cellWithTableView:(UITableView *)tableView; @end
+(instancetype)cellWithTableView:(UITableView *)tableView { static NSString *cellstr = @"newslistcell"; NewsListCell *cell=[tableView dequeueReusableCellWithIdentifier:cellstr]; if (cell==nil) { cell=[[NewsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr]; } return cell; } #pragma mark - 重写初始化方法 -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) { _NewsTitle=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 21)]; _NewsTitle.font=[UIFont boldSystemFontOfSize:15]; [self.contentView addSubview:_NewsTitle]; _NewsImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 37 ,100, 75)]; [self.contentView addSubview:_NewsImage]; _NewsDesc = [[UILabel alloc] initWithFrame:CGRectMake(120, 37, SCREEN_WIDTH-130, 60)]; _NewsDesc.numberOfLines=3; _NewsDesc.font=[UIFont systemFontOfSize:14]; _NewsDesc.textColor = [UIColor grayColor]; [self.contentView addSubview:_NewsDesc]; _NewsDate = [[UILabel alloc] initWithFrame:CGRectMake(120, 97, SCREEN_WIDTH-20, 18)]; _NewsDate.font=[UIFont systemFontOfSize:12]; _NewsDate.textColor=[UIColor grayColor]; [self.contentView addSubview:_NewsDate]; _NewsClassName =[[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-65, 98, 55, 17)]; _NewsClassName.font=[UIFont systemFontOfSize:10]; _NewsClassName.textColor = COLOR(15, 111, 172, 1); _NewsClassName.layer.borderColor = COLOR(15, 111, 172, 1).CGColor; _NewsClassName.layer.borderWidth = 0.5f; _NewsClassName.textAlignment=NSTextAlignmentCenter; [self.contentView addSubview:_NewsClassName]; } return self; } #pragma mark - 重构函数 -(void)setNews:(NewsListModel *)modnews { _news = modnews; _NewsTitle.text = modnews.NewsTitle; _NewsDate.text = modnews.NewsDate; _NewsClassName.text = modnews.NewsClassName; //_NewsDesc.text = modnews.NewsDesc; [_NewsImage sd_setImageWithURL:[NSURL URLWithString:modnews.NewsImage] placeholderImage:[UIImage imageNamed:@"defaultimage"]]; //测试代码设置行高 if ([modnews.NewsDesc isEqualToString:@""] == NO) { NSMutableAttributedString *attributedstring =[[NSMutableAttributedString alloc] initWithString:modnews.NewsDesc]; NSMutableParagraphStyle *paragraphstyle = [[NSMutableParagraphStyle alloc] init]; [paragraphstyle setLineSpacing:2.8f]; [attributedstring addAttribute:NSParagraphStyleAttributeName value:paragraphstyle range:NSMakeRange(0, [modnews.NewsDesc length])]; [_NewsDesc setAttributedText:attributedstring]; }else{ _NewsDesc.text = @""; } }
3、UrlDefine.h文件定义一下请求的url地址
#define BASE_URL_NEWS_LIST @"/news.ashx?action=list"
4、HttpHelper中添加获取新闻资讯列表的方法
#pragma mark - 获取新闻资讯列表 -(void)GetNewsListWithHUD:(BOOL)ishud andDict:(NSMutableDictionary *)dict;
#pragma mark - 获取新闻资讯列表 -(void)GetNewsListWithHUD:(BOOL)ishud andDict:(NSMutableDictionary *)dict { [self postDataWithURLString:[NSString stringWithFormat:@"%@%@",BASE_URL,BASE_URL_NEWS_LIST] andParameters:dict andHUD:ishud andFlag:@"News_List"]; }
5、.h头文件定义变量
#import <UIKit/UIKit.h> @interface RDNewsListViewController : UIViewController @property(nonatomic,strong) UITableView *tableView; @end
6、.m文件定义变量
@interface RDNewsListViewController ()<UITableViewDataSource,UITableViewDelegate,HttpHelperDelegate> { HttpHelper *helper; NSMutableArray *arrNewsList; NSMutableDictionary *searchdict; NSUInteger pageindex; NSUInteger pagesize; } @end
7、变量初始化
//初始化 self.title=@"新闻资讯"; arrNewsList = [NSMutableArray array]; helper=[HttpHelper httpHelper]; helper.delegate=self; searchdict = [NSMutableDictionary dictionary]; pagesize=15; pageindex=1; //提交请求 [searchdict setObject:[NSString stringWithFormat:@"%lu",pageindex] forKey:@"pageindex"]; [searchdict setObject:[NSString stringWithFormat:@"%lu",pagesize] forKey:@"pagesize"]; //此行也可以不要,后台默认15条数据每页 [helper GetNewsListWithHUD:YES andDict:searchdict];
8、页面初始化tableview
//初始化tableviwe _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain]; _tableView.rowHeight = BASE_HEIGHT_NEWSLISTCELL; _tableView.dataSource=self; _tableView.delegate=self; if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) { [_tableView setSeparatorInset:UIEdgeInsetsZero];} if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) { [_tableView setLayoutMargins:UIEdgeInsetsZero];} [self.view addSubview:_tableView];
9、实现httpHelper的代理方法
#pragma mark - 实现httphelper代理 -(void)HttpExecuteFailure:(AFHTTPRequestOperation *)operation andError:(NSError *)error andFlag:(NSString *)flag { NSLog(@"ERROR=%@",error); [self.view makeToast:@"服务器错误,请稍候再试。" duration:3.0 position:CSToastPositionCenter]; } -(void)HttpExecuteSuccess:(AFHTTPRequestOperation *)operation andResponseObject:(id)responseObject andFlag:(NSString *)flag { //NSLog(@"SUCCESS=%@",responseObject); NSDictionary *dict = (NSDictionary *)responseObject; if (1 == [[dict objectForKey:@"success"] integerValue]) { [self initDataWithDict:dict]; }else{ [self.view makeToast:[dict objectForKey:@"msg"] duration:3.0 position:CSToastPositionCenter]; } } -(void)initDataWithDict:(NSDictionary *)dict { //如果获取的是第一页的数据,表示刚进入该view或者刷新该view if (pageindex==1) { [arrNewsList removeAllObjects]; } NSArray *arrnews = [dict objectForKey:@"NewsList"]; for (NSDictionary *dic in arrnews) { NewsListModel *modnews = [[NewsListModel alloc]initWithDict:dic]; [arrNewsList addObject:modnews]; } [_tableView reloadData]; }
10、实现tableview的代理方法
#pragma mark - 实现tableview代理方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NewsListCell *cell=[NewsListCell cellWithTableView:tableView]; NewsListModel *modnews = arrNewsList[indexPath.row]; cell.news =modnews; return cell; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return arrNewsList.count; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; NewsListModel *modnews = arrNewsList[indexPath.row]; RDNewsDetailViewController *detail = [[RDNewsDetailViewController alloc]init]; detail.NewsID=modnews.NewsID; self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil]; [self.navigationController pushViewController:detail animated:YES]; } -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero];} if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero];} } #pragma mark - 隐藏hud和网络 -(void)viewWillDisappear:(BOOL)animated { [[HttpHelper httpHelper] CancelAllRequest]; [ToolsHelper CloseHUD]; [super viewWillDisappear:animated]; }