泉州網(wǎng)頁搜索排名提升杭州網(wǎng)站建設(shè)方案優(yōu)化
文章目錄
- 一、獲取新聞額外信息
- 二、工具欄按鈕的布局
- 三、評論區(qū)文字高度
- 四、評論區(qū)長評論和短評論的數(shù)目顯示
- 五、評論區(qū)的cell布局問題和評論消息的判斷
一、獲取新聞額外信息
新聞額外信息的URL需要通過當(dāng)前新聞的id來獲取,所以我將所有的新聞放到一個數(shù)組中,通過數(shù)組的索引來獲取指定新聞的id,而為了分辨點擊cell加載和側(cè)滑加載指定的新聞網(wǎng)頁,我設(shè)置了一個flag值(初始值為0),當(dāng)點擊時或者側(cè)滑時判斷flag值并進行相應(yīng)的操作。
- (void)GetExtraData {NSInteger index = self.mainWebView.mainwebScrollView.contentOffset.x / WIDTH;if (flag == 0) {index = self.indexValue;flag = 1;}NSLog(@"index = %ld", index);self.idStr = [self.webAllDataArray[index] id];NSLog(@"id:%ld",self.idStr);[[ExtraManager sharedSingleton] ExtraGetWithData:^(GetExtraModel * _Nullable extraModel) {dispatch_async(dispatch_get_main_queue(), ^{self.mainWebView.discussLabel.text = [NSString stringWithFormat:@"%ld", extraModel.comments];self.mainWebView.likeLabel.text = [NSString stringWithFormat:@"%ld", extraModel.popularity];self.longComments = extraModel.long_comments;self.shortComments = extraModel.short_comments;NSLog(@"額外信息獲取成功");});} andError:^(NSError * _Nullable error) {NSLog(@"額外信息獲取失敗");} andIdStr:self.idStr];
}
二、工具欄按鈕的布局
工具欄按鈕如果使用UIBarButtonItem類的對象作為屬性的話,在view層中進行創(chuàng)建和布局后是無法在viewController層中添加協(xié)議函數(shù)的,并且添加的按鈕的顏色會被修改為藍色,因此需要使用UIBUtton類的對象作為屬性,并且將UIBUtton對象作為UIBarButtonItem對象的初始化CustomView
//view層NSArray* imageStrArray = [NSArray arrayWithObjects:@"zuosanjiao.png",@"shuxian.png", @"pinglun.png", @"dianzan.png", @"shoucang.png", @"fenxiang.png", nil];
self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];[self.backButton setImage:[UIImage imageNamed:imageStrArray[0]] forState:UIControlStateNormal];self.backButton.frame = CGRectMake(0, 0, 30, 30);UIBarButtonItem* mybackButton = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.buttonArray = [NSArray arrayWithObjects:mybackButton, self.lineButton, discussButton,discussLabelbutton, btnF, likeButton,likeLabelButton, btnF, collectButton,btnF, shareButton, nil];
//viewController層
[self.mainWebView.backButton addTarget:self action:@selector(pressBack) forControlEvents:UIControlEventTouchUpInside];
self.toolbarItems = self.mainWebView.buttonArray;
三、評論區(qū)文字高度
在自定義cell中用Masonry庫進行布局時只要不設(shè)置Label控件的高度,那么Label控件的高度就會和它的文字高度一樣。
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(60);make.top.equalTo(self.authorLabel.mas_bottom).offset(10);make.width.equalTo(@(WIDTH - 70));
// make.height.equalTo(@150);}];
四、評論區(qū)長評論和短評論的數(shù)目顯示
在自定義cell中創(chuàng)建并初始化顯示評論的Label控件,需要注意的就是如果默認tableview的重用機制的話就會導(dǎo)致你的cell上會重復(fù)顯示評論的數(shù)目,因此需要改變它的重用機制,我是每次都從當(dāng)前的index path中獲取cell,這樣就會避免cell的重用導(dǎo)致的重復(fù)顯示問題
DiscussCustomCell* cell = [self.discussView.tableview cellForRowAtIndexPath:indexPath];if (cell == nil) {cell = [[DiscussCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myDiscussCustomCell"];}if (indexPath.section == 0 && self.longComments != 0) {if (indexPath.row == 0) {cell.commentsNumLabel.text = [NSString stringWithFormat:@"%ld條長評", self.longComments];}//‘’‘
}
五、評論區(qū)的cell布局問題和評論消息的判斷
使用Masonry庫進行布局時,需要控制cell中每個控件的位置,確保當(dāng)展開全文和收起改變cell的高度時不會讓自定義cell上的控件的位置錯亂,比如評論消息和被回復(fù)的評論之間的間距要固定,評論消息和名稱之間的間距也要固定
[self.commentsNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(10);make.top.equalTo(self.contentView).offset(5);make.width.equalTo(@WIDTH);make.height.equalTo(@20);}];[self.touxiangImageView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(10);make.top.equalTo(self.commentsNumLabel.mas_bottom).offset(10);make.width.equalTo(@40);make.height.equalTo(@40);}];[self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(60);make.top.equalTo(self.commentsNumLabel.mas_bottom).offset(10);make.width.equalTo(@200);make.height.equalTo(@30);}];[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(60);make.top.equalTo(self.authorLabel.mas_bottom).offset(10);make.width.equalTo(@(WIDTH - 70));
// make.height.equalTo(@150);}];[self.replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(60);make.top.equalTo(self.contentLabel.mas_bottom).offset(10);make.width.equalTo(@(WIDTH - 70));}];
有的評論它是自己發(fā)出的,有的評論它是回復(fù)別人的評論的,還有就是有時候回復(fù)別人的評論時,別人的評論已經(jīng)被刪除的情況,因此就需要進行判斷。先通過請求數(shù)據(jù)的數(shù)組中的字典元素是否存在來判斷是否回復(fù)別人的評論,再通過字典的author屬性是否為NULL判斷,評論是否已經(jīng)被刪除
if ([self.discussModel.longCommentsArray[indexPath.row] reply_to] != nil) {NSString *repliedAuthor = [self.discussModel.longCommentsArray[indexPath.row] reply_to][@"author"];NSString *repliedContent = [self.discussModel.longCommentsArray[indexPath.row] reply_to][@"content"];NSString* repliedText = [NSString stringWithFormat:@"//%@: %@", repliedAuthor, repliedContent];if (repliedAuthor != NULL) {cell.replyLabel.text = repliedText;cell.replyLabel.numberOfLines = 2;} else {cell.replyLabel.text = @"抱歉,原點評已經(jīng)被刪除";}}