ios objective-c xcode 知乎日报第一周总结 - [self-&gt

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力2027 👁️ 429 次阅读

一. 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

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

重写- (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的宽。

在- (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

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

在调用这个函数之前,我们需要将上文中的- (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的一个类,这个操作是可以的。

然后再调用上函数,直接返回的标题。

- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if (section > 1) {

return [DateTool getMonthAndDayWithTimeString:_lastDateArray[section - 1]];

}

return @"";

}

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

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

这里返回的字符串被设置到了第一个函数返回的UITableViewHeaderFooterView的textLabel属性的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是我封装的专门网络请求的单例。

精彩链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧