IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 知乎日报第一周总结 -> 正文阅读

[移动开发]知乎日报第一周总结

一. UITableView每组cell间的间距和小标题的设置。

关于这个,我在网上搜到的糊里糊涂的,所以做个总结。

首先是设置每组cell的间距

这里有四个函数:

每组cell头间距:

  • - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  • - (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

每组cell尾间距:

  • - (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  • - (UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

以设置头间距为例(尾间距同理)

  1. 重写- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section函数返回一个view。
- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 0)];
    return view;
}

这里我们只需要设置这个view的宽。

  1. - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section里返回高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        // 第一组没有头间距,让第一组cell和tableView顶部紧贴
        return 0;
    } else if (section == 1) {
        // 第二组和第一组间距为5
        return 5;
    }
    // 剩余间距为35
    return 35;
}

下面只是我的猜测,这里设置的就是第一个函数返回的view的高度。如果我们不调用第一个函数,那个view就会返回nil,所以我们只调用这个函数是不行的。而在第一个函数返回view后,其高度会被统一初始化为一个值,所以第一个函数设置的view高度是无效的,需要我们在这里重新设置。有大佬路过就指正一下。

设置每组cell标题

这里有两个函数:

头标题:- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

尾标题:- (NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

同样以头标题为例(尾标题同理):

  1. 在调用这个函数之前,我们需要将上文中的- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section中返回的View改为UITableViewHeaderFooterView类型。
- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 0)];
    return view;
}

UITableViewHeaderFooterView是继承自UIView的一个类,这个操作是可以的。
在这里插入图片描述

  1. 然后再调用上函数,直接返回的标题。
- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section > 1) {
        return [DateTool getMonthAndDayWithTimeString:_lastDateArray[section - 1]];
    }
    return @"";
}

这里不能返回nil,我的程序会崩溃,原因未知,,,,

UITableViewHeaderFooterView应该是苹果专门为tableView头尾制作的类,还可以像cell一样进行自定义,注册和复用,不过这里我比较懒,就以后再学吧,,,,

这里返回的字符串被设置到了第一个函数返回的UITableViewHeaderFooterViewtextLabel属性的text里。UIView没有这个属性,所以在第一个函数没改的话,这里就没用了。

在每组cell间画UI

理论上在第一个函数,创建UITableViewHeaderFooterView的时候画就可以,或者也可以自定义一个UITableViewHeaderFooterView。不过我就只是加了条横线做分割,就懒的自定义了。

还有就是第一个函数返回后,可能会对UITableViewHeaderFooterView的属性统一初始化。比如我们在第一个函数内修改UITableViewHeaderFooterView的textLable的字体大小是无效的。所以我在另一个函数内设置了它。

- (void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

顾名思义,就是在headerView将显示是会调用这个函数,我把UI设计(比较简单,复制的还是自定义吧)写在了这里。

- (void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (section > 1) {
        UITableViewHeaderFooterView* header = (UITableViewHeaderFooterView*)view;
        header.textLabel.font = [UIFont systemFontOfSize:16];

        UIView* lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor grayColor];

        [header.contentView addSubview:lineView];

        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0);
            make.left.mas_equalTo(100);
            make.height.mas_offset(1);
            make.centerY.mas_equalTo(0);
        }];
    }
}

值得注意的是,我们给UITableViewHeaderFooterView添加子视图也是添加在它的contentView属性上。

效果图:
在这里插入图片描述

在这里插入图片描述

我朋友说这个问题其实也可以专门设置一种cell达成同样的效果,不过我没试过。

二. 串行请求数据

我需要连续请求三天数据,且保证这三天数据是按顺序添加到一个总的数组中。我的解决方法很简单,请求嵌套请求,我承认这么写不优雅,但是它解决了我的问题。而且只是固定请求三天数据,不需要嵌套多少次。

	LastStoriesModel* lastStoriesModel = [_lastStoriesModelArray lastObject];
    NSString* lastDate1 = lastStoriesModel.date;
    NSString* lastDate2 = [DateTool dateMinusOneWhithTimeString:lastDate1];
    NSString* lastDate3 = [DateTool dateMinusOneWhithTimeString:lastDate2];
    
    [_manage getLastTime:lastDate1 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
        [self->_lastStoriesModelArray addObject:lastStoriesModel];
        [self sendStoriserToView:lastStoriesModel];
        
        [self->_manage getLastTime:lastDate2 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
            [self->_lastStoriesModelArray addObject:lastStoriesModel];
            [self sendStoriserToView:lastStoriesModel];
            
            [self->_manage getLastTime:lastDate3 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
                [self->_lastStoriesModelArray addObject:lastStoriesModel];
                [self sendStoriserToView:lastStoriesModel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->_interFaceView viewInit];
                });
            } error:^(NSError * _Nonnull error) {
                NSLog(@"getLastModel error");
            }];
        } error:^(NSError * _Nonnull error) {
            NSLog(@"getLastModel error");
        }];
    } error:^(NSError * _Nonnull error) {
        NSLog(@"getLastModel error");
    }];

DateTool是我处理时间字符串的工具类,manage是我封装的专门网络请求的单例。

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-10-31 12:09:06  更:2022-10-31 12:12:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 21:04:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码