发布时间 : 星期一 文章基于oc实现阅读器文档1更新完毕开始阅读559b6f17c4da50e2524de518964bcf84b9d52d87
4.3 搜索界面
根据搜索结果添加书本到书架
5.编码实现
1.书架
implementationBookshelfViewController
staticNSString * constreuseIdentifier = @\- (void)viewDidLoad { [superviewDidLoad]; deleteBtnFlag = YES;
self.collectionView.backgroundColor = [UIColorwhiteColor];
[selfaddDoubleTapGesture]; [selfsetupDataBase];}
- (void)viewWillAppear:(BOOL)animated{
booksArr = [YTBookItemreadDatabase];//添加最后一项,是一个带加号的图 YTBookItem *itm = [[YTBookItemalloc]init]; itm.imageKey = @\[booksArraddObject:itm];
[self.collectionViewreloadData];}
#pragma mark
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section {
returnUIEdgeInsetsMake(13,8,10,8); }
- (NSInteger)collectionView:(UICollectionView
*)collectionViewnumberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items returnbooksArr.count; }
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath*)indexPath {
YTBookCollectionViewCell *cell =
[collectionViewdequeueReusableCellWithReuseIdentifier:reuseIdentifierforIndexPath:indexPath];
YTBookItem *item = booksArr[indexPath.row];
cell.imageView.image = [[SDImageCachesharedImageCache] imageFromDiskCacheForKey:item.imageKey];
cell.bookNameView.text = item.name;//如果小说没有封面,就使用默认图 if (cell.imageView.image == nil) {
cell.imageView.image = [UIImageimageNamed:@\如果是最后一项,则显示加号图
if ([item.imageKeyisEqualToString:@\
cell.imageView.image = [UIImageimageNamed:@\cell.indexPath = indexPath;
cell.deleteBtn.hidden = deleteBtnFlag?YES:NO; cell.delegate= self; return cell;} 2.书城
#pragma mark
*)collectionViewshouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;}
- (void)collectionView:(UICollectionView
*)collectionViewdidSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//如果点击最后一项,就跳转到书城界面 if (indexPath.row == booksArr.count -1 ) { self.tabBarController.selectedIndex = 1; }else{
if (indexPath.row == booksArr.count -1 ) { self.tabBarController.selectedIndex = 1; }else{
// 需要参数 id url md b.a(authoer)cmdb.n(name) loceid // 1.由indexPath,从数组取bookitem对象
YTBookItem *bookitem = booksArr[indexPath.row]; //2.根据name拼接表名,便于查询章节数据表 NSString *tableStr =
[NSStringstringWithFormat:@\
//目前只读取第一章 NSIntegerpageIndex = 1;
NSMutableArray *chaptersArray = [NSMutableArrayarrayWithArray: [YTChaptersItemreadOneChapterFromTable:tableStrIndex:pageIndex]];
YTChaptersItem *chapterItem = [[YTChaptersItemalloc]init]; for (YTChaptersItem *chapter in chaptersArray) { chapterItem = chapter;}
globalMD5 = chapterItem.md5;
//有了bookitem, chapteritem就可以拼接url来请求数据了 if (bookitem.bkey.length> 8) {
//没有bkey的,值为\所以长度至少要大于6 才能判断 //有bkey
//有bkey和没bkey请求的url不同,要分开
// 3.从bookitem里拿 id md b.ab.mloc,这一步不用写,用在拼接url字符串里
NSString *cacheDir =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//txt文件绝对路径
NSString *TxtfilePath = [NSString
stringWithFormat:@\NSStringEncodingstrEncode =
CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *txt =
[NSStringstringWithContentsOfFile:TxtfilePathencoding:strEncodeerror:nil];
YTNovelContentController *NCVC = [[self
storyboard]instantiateViewControllerWithIdentifier:@\
if (txt) {
//如果有文件,则直接推送到阅读器打开 [NCVC loadText:txt];
[self.navigationControllerpushViewController:NCVCanimated:YES]; }else{
//如果没有文件,则执行下载操作,然后打开 NSString *urlStr =
[NSStringstringWithFormat:@\=%@&md5=%@&uid=80C5B623E2F3031DC4B1874096C54217@qq.sohu.com&token=4244558c08b4ee4e9791b06cca4ec139&eid=1136\md5];
[selfdownloadZip:urlStr]; }}else{ //没bkey
NSLog(@\
//中文作为请求参数,需要转义 bookitem.author = [bookitem.author
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
bookitem.name = [bookitem.name
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *nobkeyUrlStr = [NSString
stringWithFormat:@\&md=%@&b.a=%@&cmd=%@&b.n=%@&loc=%@&eid=1136\chapterItem.url,bookitem.md,bookitem.author,chapterItem.cmd,bookitem.name,bookitem.loc];
[selfNobkeyChapterContentRequest:nobkeyUrlStr]; 3.搜索页面
@interface YTSearchViewController (){ __block BOOL sectionHeaderFlag; __weak YTSearchFooter *weakfooter; NSUIntegerpageInt;
__block NSString *globalSearchKeyword;
__block NSUIntegerpagetotal; //请求的总页数,一旦达到了,那么下拉刷新无效
}
@property (nonatomic, strong) YTSearchBar *searchBar;
@property (nonatomic, copy) NSString *searchContent; // 搜索内容 @property (nonatomic, strong) YTSearchFooter *footer; // 推荐搜索 @property (nonatomic, strong) NSArray *hotSearchWords; // 推荐搜索关键词
@property (nonatomic,strong) NSMutableArray *resultArr; //搜索结果数组(存2个group对象)
@end
@implementation YTSearchViewController //1 懒加载
- (NSMutableArray *)resultArr{ if (_resultArr ==nil) {