因为移除收藏的时候我使用的是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++;
}
}
这是因为在收藏按钮被点击的时候我就将点赞的状态添加到传输的数组里了,导致后来再点击点赞的时候,因为已经把之前的状态添加过了,所以它的状态不会被改变,解决方法就是更新传输数据的数组,当你点击点赞按钮时,对传输数组进行判断,如果其不为空就使用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];
}
}
}
}
}
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
//.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];
在自定义的cell
中对UILabel
不进行高度约束,它就会自适应高度,让UILabel的高度与文字刚好一样!!!感谢我的树懒学姐的精心指导!!!
iOS获取Label高度的几种方法与对比
key
:if ([dic objectForKey:@"yourKey"]) {
NSLog(@"有这个值");
} else {
NSLog(@"没有这个值");
}
UIButton
点击事件没有反应:因为我的cell
设置的为不可选中,button
受到了影响。
self.messageCell.userInteractionEnabled = NO;
改为:
self.messageCell.userInteractionEnabled = YES;
self.messageCell.selectionStyle = UITableViewCellAccessoryNone;
用一个数组来存储它是否展开的状态,然后添加按钮的点击事件,将它的数组中的相应索引的值进行改变,然后重新加载tableView
,注意的是,在你写tableView
的cell
中的回复评论的赋值的时候就得对其进行判断是否为展开状态然后在赋值加载。
//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];
}
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;
}