cell的点击事件需要通过考虑组数与行数的数量关系来确定出来点击得到底是哪一个stories的cell,从而加载到正确的stories中。
//点击事件的方法
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath: indexPath animated: YES];
if (indexPath.section > 0) {
StoriesViewController* stories = [[StoriesViewController alloc] init];
stories.x = (indexPath.section - 1)*5 + (indexPath.row);
stories.storiesmodel = [[StoriesModel alloc] init];
stories.storiesmodel.storiesURL = [NSMutableArray array];
[stories.storiesmodel.storiesURL addObjectsFromArray:self.storiesURL];
[self.navigationController pushViewController:stories animated:YES];
}
}
点击之后还要实现无限滑动的stories,这里采取的思路是每次加载三个视图在滑动的时候不断改变中间视图的数据信息就可以实现无限滑动的效果了,基本思路就是无限轮播图的思想。
//实现点击之后的无限轮播
- (void) loadScrollView {
self.storiesview.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.storiesview.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 3, self.view.bounds.size.height);
self.storiesview.scrollView.pagingEnabled = YES;
self.storiesview.scrollView.scrollEnabled = YES;
self.storiesview.scrollView.showsVerticalScrollIndicator = NO;
self.storiesview.scrollView.showsHorizontalScrollIndicator = NO;
self.storiesview.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 1, 0);
self.storiesview.scrollView.delegate = self;
[self.view addSubview:self.storiesview.scrollView];
UIView* leftView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
UIView* midView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 1, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
UIView* rightView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 2, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
leftView.backgroundColor = UIColor.systemGrayColor;
midView.backgroundColor = UIColor.systemGray3Color;
rightView.backgroundColor = UIColor.systemGray5Color;
[self.storiesview.scrollView addSubview:leftView];
[self.storiesview.scrollView addSubview:midView];
[self.storiesview.scrollView addSubview:rightView];
self.storiesmodel.viewArray = [NSMutableArray arrayWithObjects:leftView, midView, rightView, nil];
}
- (void)loadViews {
if (self.x == 0) {
self.x++;
self.storiesview.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 0, 0);
} else if (self.x == self.storiesmodel.storiesURL.count - 1){
self.x--;
self.storiesview.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 2, 0);
} else {
self.storiesview.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 1, 0);
}
[self loadWebViewWithURL:self.storiesmodel.storiesURL[self.x - 1] andView:self.storiesmodel.viewArray[0]];
[self loadWebViewWithURL:self.storiesmodel.storiesURL[self.x] andView:self.storiesmodel.viewArray[1]];
[self loadWebViewWithURL:self.storiesmodel.storiesURL[self.x + 1] andView:self.storiesmodel.viewArray[2]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
NSLog(@"%f", offsetX);
if (offsetX == self.view.bounds.size.width * 0 && self.x != 1) {
self.x--;
UIView *leftView, *midView, *rightView;
leftView = self.storiesmodel.viewArray[0];
midView = self.storiesmodel.viewArray[1];
rightView = self.storiesmodel.viewArray[2];
leftView.frame = CGRectMake(self.view.bounds.size.width * 1, 0, self.view.bounds.size.width, self.view.bounds.size.height);
midView.frame = CGRectMake(self.view.bounds.size.width * 2, 0, self.view.bounds.size.width, self.view.bounds.size.height);
rightView.frame = CGRectMake(self.view.bounds.size.width * 0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
self.storiesview.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 1, 0);
[rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self loadWebViewWithURL:self.storiesmodel.storiesURL[self.x - 1] andView:rightView];
self.storiesmodel.viewArray = [[NSMutableArray alloc] initWithObjects:rightView, leftView, midView, nil];
} else if (offsetX == self.view.bounds.size.width * 2 && self.x != self.storiesmodel.viewArray.count - 2) {
self.x++;
UIView *leftView, *midView, *rightView;
leftView = self.storiesmodel.viewArray[0];
midView = self.storiesmodel.viewArray[1];
rightView = self.storiesmodel.viewArray[2];
leftView.frame = CGRectMake(self.view.bounds.size.width * 2, 0, self.view.bounds.size.width, self.view.bounds.size.height);
midView.frame = CGRectMake(self.view.bounds.size.width * 0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
rightView.frame = CGRectMake(self.view.bounds.size.width * 1, 0, self.view.bounds.size.width, self.view.bounds.size.height);
self.storiesview.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width * 1, 0);
[leftView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self loadWebViewWithURL:self.storiesmodel.storiesURL[self.x + 1] andView:leftView];
self.storiesmodel.viewArray = [[NSMutableArray alloc] initWithObjects:midView, rightView, leftView, nil];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%ld", self.storiesmodel.storiesURL.count);
self.storiesview = [[storiesView alloc] init];
[self loadScrollView];
[self loadViews];
[self loadtabBarItem];
}
点赞以及评论的数据,首先和前面的是一样的先要封装一个网络请求,然后请求到点赞以及评论的数据,之后把请求的数据添加到label上然后设置label的位置从而把数据添加到上面。
因为topstories也是一种轮播图,所以我在这里通过flage来判断他是首次请求数据还是滑动之后请求数据。
- (void) getdata {
NSInteger index = self.topstoriesView.scrollview.contentOffset.x/Width;
if (self.flage == 0) {
index = self.num;
self.flage = 1;
}
NSString* str = self.Model.idnum[index];
NSLog(@"%@", str);
[[topMananger shareManger] makeData:^(topextra * _Nonnull ViewModel) {
self.Model.poular = [ViewModel popularity];
NSLog(@"%ld", self.Model.poular);
self.Model.comments = [ViewModel comments];
NSLog(@"%ld", self.Model.comments);
dispatch_async(dispatch_get_main_queue(), ^{
//返回
UIButton* btnImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btnImage1 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal];
btnImage1.frame = CGRectMake(0, 0, 44, 44);
[btnImage1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
btnImage1.userInteractionEnabled = YES;
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:btnImage1];
//评论
UIButton* btnImage2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btnImage2 setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
btnImage2.frame = CGRectMake(0, 0, 44, 44);
UILabel *badgeLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 30, 30)];
badgeLabel1.backgroundColor = [UIColor clearColor];
badgeLabel1.textColor = [UIColor blackColor];
badgeLabel1.textAlignment = NSTextAlignmentCenter;
badgeLabel1.font = [UIFont systemFontOfSize:12];
badgeLabel1.layer.cornerRadius = 10;
badgeLabel1.layer.masksToBounds = YES;
NSNumber* number1 = @(self.Model.comments);
NSString* text1 = [number1 stringValue];
badgeLabel1.text = text1;
[btnImage2 addSubview:badgeLabel1];
UIBarButtonItem* commentButton = [[UIBarButtonItem alloc] initWithCustomView:btnImage2];
//点赞
UIButton* btnImage3 = [UIButton buttonWithType:UIButtonTypeCustom];
[btnImage3 setImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal];
btnImage3.frame = CGRectMake(0, 0, 44, 44);
UILabel *badgeLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 30, 30)];
badgeLabel2.backgroundColor = [UIColor clearColor];
badgeLabel2.textColor = [UIColor blackColor];
badgeLabel2.textAlignment = NSTextAlignmentCenter;
badgeLabel2.font = [UIFont systemFontOfSize:12];
badgeLabel2.layer.cornerRadius = 10;
badgeLabel2.layer.masksToBounds = YES;
NSNumber* number2 = @(self.Model.poular);
NSString* text2 = [number2 stringValue];
badgeLabel2.text = text2;
[btnImage3 addSubview:badgeLabel2];
UIBarButtonItem* likeButton = [[UIBarButtonItem alloc] initWithCustomView:btnImage3];
//收藏
UIButton* btnImage4 = [UIButton buttonWithType:UIButtonTypeCustom];
[btnImage4 setImage:[UIImage imageNamed:@"9.png"] forState:UIControlStateNormal];
btnImage4.frame = CGRectMake(0, 0, 44, 44);
UIBarButtonItem* favouriteButton = [[UIBarButtonItem alloc] initWithCustomView:btnImage4];
UIBarButtonItem* btnF02 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray* array = [NSArray arrayWithObjects:backButton,btnF02,commentButton,btnF02,likeButton,btnF02,favouriteButton,nil];
self.toolbarItems = array;
});
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败");
} andstring:(id) str];
}
这周因为某些原因进度有点慢,下一周要赶一下进度。