【iOS】——知乎日报第一周总结

文章目录


一、框架和布局问题

仿写知乎日报用到的框架依旧是MVC框架,所以一开始要想好该怎么搭建大体框架,对于各个模块该怎么分配,需要用到哪些传值。对于布局,因为要用到导航栏的相关UI元素所以,我用的是将导航控制器作为根视图控制器再将导航控制器作为我自定义的视图控制器的根视图控制器。由于导航栏的UI元素不好布局所以我创建了一个导航视图并赋值给了导航栏的titleView属性。

objectivec 复制代码
UINavigationBarAppearance* appearance = [[UINavigationBarAppearance alloc] init];
        appearance.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.standardAppearance = appearance;
    self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    self.navigationItem.titleView = self.zhihuView.titleView;

布局用到的有系统库中自带的方法和Masonry库的方法,两个搭配着使用效果要更好。

二、线程冲突问题

当我使用Block传值过来的网络请求的数据赋值给我布局的UI元素时,运行时总是会导致程序崩掉,查了下原因是进行完赋值后程序还没回到主线程进行UI布局,所以我加了下面这个方法让程序回到主线程进行布局。

objectivec 复制代码
 dispatch_async(dispatch_get_main_queue(), ^{
//
 });

三、下拉刷新问题

这里我使用了第三方库MJRefresh库进行下拉刷新,当我下拉刷新时,程序会调用这个库的库方法,接着我在库方法中给我创建的下拉数组添加新的元素,这里我设置的是组元素也就是每下拉刷新一次,添加一个元素,接着在tableView控件的返回组数的协议函数里返回我创建的下拉数组的元素个数。

objectivec 复制代码
self.zhihuView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
self.currentPage = 1;
    [self loadMoreData];
    self.data = [NSMutableArray arrayWithObjects:@"Section 0", @"Section 1", nil];


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.data.count;
}

- (void)loadMoreData {
    // 模拟网络请求加载更多数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 模拟生成新的表格数据
        NSArray *newData = [self generateNewData];
        
        // 将新的数据添加到原有的数据源中
        [self.data addObjectsFromArray:newData];
        
       
        }

- (NSArray *)generateNewData {
    NSMutableArray *newData = [NSMutableArray array];
    NSInteger count = 1;
//    for (NSInteger i = 0; i < count; i++) {
        NSInteger section = self.currentPage * count + 1;
        NSString *data = [NSString stringWithFormat:@"Section %ld", (long)section];
        [newData addObject:data];
//    }
    self.currentPage++;
    return newData;
}

四、添加网络请求的图片

我通过网络请求的图片只是这个图片的字符串形式的URL地址,还需要将这个图片的URL地址转换成URL,接着通过URL才能加载图片到指定的UI控件上。这里我用到的是SDWebImage库来进行,这个库可以通过图片URL将图片加载到指定的UI控件上。

objectivec 复制代码
NSArray* contentIamgeArray = [self.contentPreDataArray[indexPath.row] images];
        NSString* contentImageStr = contentIamgeArray[0];
        NSURL* contentImageUrl = [NSURL URLWithString:contentImageStr];
        [cell02.contentImageView sd_setImageWithURL:contentImageUrl];

五、时间标题设置问题

对于知乎日报上每天的时间标题,我使用的是日期组件,时间和tableview的组数建立数学关系,这样就能实现时间标题

objectivec 复制代码
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section >= 2) {
        NSDate* titleDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-(section - 1) * 3600 * 24];
        NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        unsigned unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents* comp = [gregorian components:unitFlags fromDate:titleDate];
        NSString* dateStr = [NSString stringWithFormat:@"%ld月%ld日", comp.month, comp.day];
        return dateStr;
    }
    return @"";
}


相关推荐
用户093 小时前
如何避免写垃圾代码:iOS开发篇
ios·swiftui·swift
HarderCoder17 小时前
iOS 知识积累第一弹:从 struct 到 APP 生命周期的全景复盘
ios
叽哥1 天前
Flutter Riverpod上手指南
android·flutter·ios
用户092 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan2 天前
iOS26适配指南之UIColor
ios·swift
权咚3 天前
阿权的开发经验小集
git·ios·xcode
用户093 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间3 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918413 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
2303_Alpha3 天前
SpringBoot
笔记·学习