【iOS】—— 知乎日报第三周遇到的问题

1.当你在收藏界面取消收藏的时候,一次取消多个,有些会移除不掉,还会显示在收藏界面的cell:

因为移除收藏的时候我使用的是for循环对其收藏状态进行判断,所以它每次都会进行i++,并且我的for循环的判断标准是传输数组的count值,但是当你remove掉数组的值后,它的count值也会发生改变,所以循环会提前停止,导致有的值没有被移除掉。对它的修改就是将for循环改为while循环:

int i = 0;
    while (i < self.allTransDataArray.count) {
        if ([self.allTransDataArray[i][7] isEqualToString:@"0"]) {
            [self.allTransDataArray removeObjectAtIndex:i];
        } else {
            i++;
        }
    }

2.收藏系统,先收藏在点赞,当你切换到收藏界面时,点赞的状态并没有改变:

这是因为在收藏按钮被点击的时候我就将点赞的状态添加到传输的数组里了,导致后来再点击点赞的时候,因为已经把之前的状态添加过了,所以它的状态不会被改变,解决方法就是更新传输数据的数组,当你点击点赞按钮时,对传输数组进行判断,如果其不为空就使用for循环对它进行遍历,遍历你当时存储的它的相应的下标,能找的话就把这一组的值抽出来,对点赞按钮的状态进行改变,然后在将改变后的数据替换原位置的数据,若找不到就不对其进行操作。

- (void)pressGood:(UIButton *)button {
    if (button.selected) {
        button.selected = NO;
        [self.goodArray replaceObjectAtIndex:button.tag withObject:@"0"];
        
        NSString *locationString = [[NSString alloc] initWithFormat:@"%ld", (long)button.tag];
        if (self.allTransDataArray != nil) {
            for (int i = 0; i < self.allTransDataArray.count; i++) {
                if ([self.allTransDataArray[i][5] isEqualToString:locationString]) {
                    self.temporaryArray = [[NSMutableArray alloc] init];
                    self.temporaryArray = self.allTransDataArray[i];
                    [self.temporaryArray replaceObjectAtIndex:6 withObject:@"0"];
                    [self.allTransDataArray replaceObjectAtIndex:i withObject:self.temporaryArray];
                }
            }
        }
    } else {
        button.selected = YES;
        [self.goodArray replaceObjectAtIndex:button.tag withObject:@"1"];
        
        NSString *locationString = [[NSString alloc] initWithFormat:@"%ld", (long)button.tag];
        if (self.allTransDataArray != nil) {
            for (int i = 0; i < self.allTransDataArray.count; i++) {
                if ([self.allTransDataArray[i][5] isEqualToString:locationString]) {
                    self.temporaryArray = [[NSMutableArray alloc] init];
                    self.temporaryArray = self.allTransDataArray[i];
                    [self.temporaryArray replaceObjectAtIndex:6 withObject:@"1"];
                    [self.allTransDataArray replaceObjectAtIndex:i withObject:self.temporaryArray];
                }
            }
        }
    }
}

3.网络请求数据不成功,获取到的字典数据返回为nil

这个是因为获取的网络JSON数据要一一对应,我当时是吧objective当成了数组然后对其进行数据的获取,所以导致获取该部分数据的时候总会获取不到,所以获取的相应的字典数据就为nil,最终我将数组改为了相应的数据问题得到解决。感谢我的学姐浪费晚上两个小时复习时间来解决我的傻逼问题!!!

@protocol ShortCommentsModel
@end

@protocol ShortReplyToModel
@end

#import "JSONModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface ShortReplyToModel : JSONModel   //被回复的评论
@property (nonatomic, copy) NSString *content; //评论
@property (nonatomic, copy) NSString *author; //作者
@end

@interface ShortCommentsModel : JSONModel
@property (nonatomic, copy) NSString *author; //作者
@property (nonatomic, copy) NSString *content; //评论
@property (nonatomic, copy) NSString *avatar; //头像
@property (nonatomic, copy) NSString *time; //时间
@property (nonatomic, copy) NSArray<ShortReplyToModel> *reply_to; //被回复的评论
@end


@interface ShortJSONModel : JSONModel
@property (nonatomic, strong) NSArray<ShortCommentsModel> *comments;
@end

NS_ASSUME_NONNULL_END

改为:

@protocol ShortCommentsModel
@end

@protocol ShortReplyToModel
@end

#import "JSONModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface ShortReplyToModel : JSONModel   //被回复的评论
@property (nonatomic, copy) NSString *content; //评论
@property (nonatomic, copy) NSString *author; //作者
@end

@interface ShortCommentsModel : JSONModel
@property (nonatomic, copy) NSString *author; //作者
@property (nonatomic, copy) NSString *content; //评论
@property (nonatomic, copy) NSString *avatar; //头像
@property (nonatomic, copy) NSString *time; //时间
@property (nonatomic, copy) ShortReplyToModel *reply_to; //被回复的评论
@end


@interface ShortJSONModel : JSONModel
@property (nonatomic, strong) NSArray<ShortCommentsModel> *comments;
@end

NS_ASSUME_NONNULL_END

4.UILabel的左上对齐:

//.h文件中
#import <UIKit/UIKit.h>

@interface TopLeftLabel : UILabel

@end

//.m文件中
#import "TopLeftLabel.h"

@implementation TopLeftLabel
- (id)initWithFrame:(CGRect)frame {
    return [super initWithFrame:frame];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    textRect.origin.y = bounds.origin.y;
    return textRect;
}
-(void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}
@end

用的时候直接集成这个label就好::

NSString *string=@"为了测试label的显示方法,所以字符串的长度要长一些!!为了测试label的显示方法,所以字符串的长度要长一些!!为了测试label的显示方法,所以字符串的长度要长一些!!";
    _label = [[TopLeftLabel alloc] initWithFrame:CGRectMake(20, 50, SCREEN_WIDTH-40, 300)];
    _label.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _label.numberOfLines = 0;
    _label.text = string;
    [self.view addSubview:_label];

5.知乎日报评论自适应:

在自定义的cell中对UILabel不进行高度约束,它就会自适应高度,让UILabel的高度与文字刚好一样!!!感谢我的树懒学姐的精心指导!!!

6.计算UILabel高度:

iOS获取Label高度的几种方法与对比

7.判断一个字典里有没有一个key

if ([dic objectForKey:@"yourKey"]) {
    NSLog(@"有这个值");
} else {
    NSLog(@"没有这个值");
}

8.UIButton点击事件没有反应:

因为我的cell设置的为不可选中,button受到了影响。

self.messageCell.userInteractionEnabled = NO;

改为:

self.messageCell.userInteractionEnabled = YES;

9.改UITableViewCell的选中状态为无色:

self.messageCell.selectionStyle = UITableViewCellAccessoryNone;

10.评论展开:

用一个数组来存储它是否展开的状态,然后添加按钮的点击事件,将它的数组中的相应索引的值进行改变,然后重新加载tableView,注意的是,在你写tableViewcell中的回复评论的赋值的时候就得对其进行判断是否为展开状态然后在赋值加载。

//cell的按钮文字和是否显示,以及回复的评论状态
if ([self.longDataArray[indexPath.row] count] == 6) {
	if ([self.longOpenFlagArray[indexPath.row] isEqualToString:@"0"]) {
	    self.messageCell.replyLabel.numberOfLines = 2;
	} else {
	    self.messageCell.replyLabel.numberOfLines = 0;
	}
	NSString *replyString = [[NSString alloc] initWithFormat:@"//%@:%@", self.longDataArray[indexPath.row][4], self.longDataArray[indexPath.row][5]];
	self.messageCell.replyLabel.text = replyString;
} else {
    self.messageCell.replyLabel.text = @"";
}
self.messageCell.openButton.tag = indexPath.row * 2 + 1;
//按钮的文字
if ([self.longOpenFlagArray[indexPath.row] isEqualToString:@"0"]) {
    [self.messageCell.openButton setTitle:@"点击展开" forState:UIControlStateNormal];
} else {
    [self.messageCell.openButton setTitle:@"收起" forState:UIControlStateNormal];
}
[self.messageCell.openButton addTarget:self action:@selector(pressOpen:) forControlEvents:UIControlEventTouchUpInside];
if ([self.longReplyHeightArray[indexPath.row] floatValue] > 40) {
    self.messageCell.openButton.hidden = NO;
} else {
    self.messageCell.openButton.hidden = YES;
}

//返回的cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //获取当前cell
    MessageTableViewCell *nowCell = [self.messageTableView cellForRowAtIndexPath:indexPath];
    if (self.longNumber && self.shortNumber) {
        if (indexPath.section == 0) { //高度大于40,并且状态为没展开就将它设为2
            if ([self.longReplyHeightArray[indexPath.row] floatValue] > 40 && [self.longOpenFlagArray[indexPath.row] isEqualToString:@"0"]) {
                nowCell.replyLabel.numberOfLines = 2;
                return [self.longHeightArray[indexPath.row] floatValue] + 36 + 90;
            } else {
                nowCell.replyLabel.numberOfLines = 0;
                return [self.longHeightArray[indexPath.row] floatValue] + [self.longReplyHeightArray[indexPath.row] floatValue] + 90;
            }
        } else {
            if ([self.shorReplytHeightArray[indexPath.row] floatValue] > 40 && [self.shortOpenFlagArray[indexPath.row] isEqualToString:@"0"]) {
                nowCell.replyLabel.numberOfLines = 2;
                return [self.shortHeightArray[indexPath.row] floatValue] + 36 + 90;
            } else {
                nowCell.replyLabel.numberOfLines = 0;
                return [self.shortHeightArray[indexPath.row] floatValue] + [self.shorReplytHeightArray[indexPath.row] floatValue] + 90;
            }
        }
    } else if (self.longNumber) {
        if ([self.longReplyHeightArray[indexPath.row] floatValue] > 40 && [self.longOpenFlagArray[indexPath.row] isEqualToString:@"0"]) {
            nowCell.replyLabel.numberOfLines = 2;
            return [self.longHeightArray[indexPath.row] floatValue] + 36 + 90;
        } else {
            nowCell.replyLabel.numberOfLines = 0;
            return [self.longHeightArray[indexPath.row] floatValue] + [self.longReplyHeightArray[indexPath.row] floatValue] + 90;
        }
    } else {
        if ([self.shorReplytHeightArray[indexPath.row] floatValue] > 40 && [self.shortOpenFlagArray[indexPath.row] isEqualToString:@"0"]) {
            nowCell.replyLabel.numberOfLines = 2;
            return [self.shortHeightArray[indexPath.row] floatValue] + 36 + 90;
        } else {
            nowCell.replyLabel.numberOfLines = 0;
            return [self.shortHeightArray[indexPath.row] floatValue] + [self.shorReplytHeightArray[indexPath.row] floatValue] + 90;
        }
    }
}

//UIButton的点击事件
- (void)pressOpen:(UIButton *)button {
    if (button.tag % 2) {  //长评的
        if ([self.longOpenFlagArray[(button.tag - 1) / 2] isEqualToString:@"0"]) {
            [self.longOpenFlagArray replaceObjectAtIndex:(button.tag - 1) / 2 withObject:@"1"];
        } else {
            [self.longOpenFlagArray replaceObjectAtIndex:(button.tag - 1) / 2 withObject:@"0"];
        }
    } else {  //短评的
        if ([self.shortOpenFlagArray[(button.tag) / 2] isEqualToString:@"0"]) {
            [self.shortOpenFlagArray replaceObjectAtIndex:(button.tag) / 2 withObject:@"1"];
        } else {
            [self.shortOpenFlagArray replaceObjectAtIndex:(button.tag) / 2 withObject:@"0"];
        }
    }
    [self.messageTableView reloadData];
}

11.时间戳转时间:

iOS 时间戳、时间转换

- (NSString *)getTimeFromTimestamp:(NSString *)timeString {
    double timeValue = [timeString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeValue];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    
    NSString *changeTime = [formatter stringFromDate:date];
    return changeTime;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值