UICollectionViewCell里的Label为什么会出现文字重叠?

发布于 2022-09-01 16:32:31 字数 9658 浏览 33 评论 0

collectionView
如上图所示,cell的类型如下:

@interface ZXSchoolMenuCollectionViewCell : UICollectionViewCell
@property (nonatomic , weak) IBOutlet UIImageView *iconImage;
@property (nonatomic , weak) IBOutlet UILabel *nameLabel;
@property (nonatomic , weak) IBOutlet UIView *virticalLine;
@property (nonatomic , weak) IBOutlet UIView *horizonLine;
@property (nonatomic , weak) IBOutlet UIView *badgeView;
@end

viewcontroller的实现

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.schoolImageView.layer.contentsGravity = kCAGravityResizeAspectFill;
    self.schoolImageView.layer.masksToBounds = YES;
    [self.tableView setSeparatorColor:[UIColor colorWithRed:237/255.0 green:235/255.0 blue:229/255.0 alpha:1.0]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSuccess:) name:@"changeSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editSchool) name:changeSchoolNotification object:nil];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"切换学校" style:UIBarButtonItemStylePlain target:self action:@selector(moreAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    MBProgressHUD *hud = [MBProgressHUD showWaiting:@"获取身份" toView:self.view];
    [ZXAccount getLoginStatusWithUid:[ZXUtils sharedInstance].user.uid block:^(ZXAccount *account , NSError *error) {
        [hud hide:YES];
        if (!error) {
            [ZXUtils sharedInstance].account = account;
            NSDictionary *dic = [account keyValues];
            [GVUserDefaults standardUserDefaults].account = dic;
            if (account.logonStatus == 1) {
                ZXBlankSchoolViewController *vc = [ZXBlankSchoolViewController viewControllerFromStoryboard];
                vc.view.frame = self.view.bounds;
                [self.view addSubview:vc.view];
            } else if (account.logonStatus == 2) {
                [self performSegueWithIdentifier:@"change" sender:nil];
            }
            
            
            [self configureUIWithSchool:[ZXUtils sharedInstance].currentSchool];
            [self configureDataArray];
            [self.tableView reloadData];
            
            [self setTags:account.tags];
            [self getUnreadMessageNum];
        }
    }];
    
    [[EaseMob sharedInstance].chatManager addDelegate:self
                                        delegateQueue:nil];
    [self.tableView setExtrueLineHidden];
}

- (void)getUnreadMessageNum
{
    //TODO: 先注释掉,会出现文字重叠
    [ZXDynamicMessage getNewSchoolDynamicMessageWithUid:GLOBAL_UID sid:[ZXUtils sharedInstance].currentSchool.sid block:^(NSInteger newMessageNum, NSError *error) {
        unreadNum = newMessageNum;
        [self.tableView reloadData];
    }];
}

- (void)editSchool
{
    [self configureUIWithSchool:[ZXUtils sharedInstance].currentSchool];
}

- (void)setTags:(NSString *)tags
{
    NSArray *arr = [tags componentsSeparatedByString:@","];
    NSSet *set = [NSSet setWithArray:arr];
    [APService setTags:[APService filterValidTags:set] alias:[ZXUtils sharedInstance].user.account callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:NO animated:YES];
    [self.navigationController.navigationBar setHidden:NO];
    
    if ([ZXUtils sharedInstance].currentSchool) {
        [self getUnreadMessageNum];
    }
}

- (void)changeSuccess:(NSNotification *)notification
{
    [self configureUIWithSchool:[ZXUtils sharedInstance].currentSchool];
    [self configureDataArray];
    [self.tableView reloadData];
}

- (IBAction)moreAction:(id)sender
{
    [self performSegueWithIdentifier:@"change" sender:sender];
}

- (void)configureDataArray
{
    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"校园动态",@"校园公告",@"校园简介",@"教师风采",@"打卡记录",@"我的IC卡", nil];
    
    NSArray *menuArray;
    ZXIdentity identity = [[ZXUtils sharedInstance] getHigherIdentity];
    if (identity == ZXIdentityParent) {
        menuArray = @[@"教工列表"];
    } else if (identity == ZXIdentityStaff) {
        menuArray = @[@"组织架构"];
    } else {
        menuArray = @[@"组织架构",@"班级列表"];
    }
    
    [self.dataArray insertObjects:menuArray atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(4, menuArray.count)]];
}

#pragma mark- tableview delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 201;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 7;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak __typeof(&*self)weakSelf = self;
    ZXSchoolMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZXSchoolMenuTableViewCell"];
    [cell configureWithDataArray:self.dataArray unreadNum:unreadNum];
    cell.selectIndexBlock = ^(NSInteger index) {
        NSString *string = weakSelf.dataArray[index];
        if ([string isEqualToString:@"校园动态"]) {
            ZXSchollDynamicViewController *vc = [ZXSchollDynamicViewController viewControllerFromStoryboard];
            vc.unreadCount = unreadNum;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else if ([string isEqualToString:@"校园公告"]) {
            ZXAnnouncementViewController *vc = [ZXAnnouncementViewController viewControllerFromStoryboard];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else if ([string isEqualToString:@"校园简介"]) {
            ZXSchoolSummaryViewController *vc = [ZXSchoolSummaryViewController viewControllerFromStoryboard];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else if ([string isEqualToString:@"教师风采"]) {
            ZXTeacherGracefulViewController *vc = [ZXTeacherGracefulViewController viewControllerFromStoryboard];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else if ([string isEqualToString:@"打卡记录"]) {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ICCard" bundle:nil];
            NSString *vcName = @"ZXCardHistoryMenuViewController";
            UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:vcName];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else if ([string isEqualToString:@"我的IC卡"]) {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ICCard" bundle:nil];
            UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ZXMyCardViewController"];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        } else if ([string isEqualToString:@"组织架构"]) {
            ZXTeachersViewController *vc = [ZXTeachersViewController viewControllerFromStoryboard];
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            ZXClassListViewController *vc = [ZXClassListViewController viewControllerFromStoryboard];
            [self.navigationController pushViewController:vc animated:YES];
        }
    };
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    CGFloat ImageWidth = SCREEN_WIDTH;
    CGFloat ImageHeight = 215;
    if (offsetY < 0) {
        CGFloat factor = ((ABS(offsetY)+ImageHeight)*ImageWidth)/ImageHeight;
        CGRect f = CGRectMake(-(factor-ImageWidth)/2, offsetY, factor, ImageHeight+ABS(offsetY));
        self.schoolImageView.layer.frame = f;
    } else {
        CGFloat ImageWidth = self.schoolImageView.frame.size.width;
        CGFloat ImageHeight = self.schoolImageView.frame.size.height;
        CGRect f = CGRectMake(0, 0, ImageWidth, ImageHeight);
        self.schoolImageView.layer.frame = f;
        
    }
}

- (void)configureUIWithSchool:(ZXSchool *)school
{
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:[[ZXImageUrlHelper imageUrlForSchoolImage:school.img].absoluteString stringByReplacingOccurrencesOfString:@"small" withString:@"origin"]] options:SDWebImageRetryFailed|SDWebImageLowPriority progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
    
        if (!image) {
            image = [UIImage imageNamed:@"mine_profile_bg"];
        }
//        UIImage *blurImage = [self blureImage:image withInputRadius:5];
//        if (blurImage) {
            [self.schoolImageView setImage:image];
//        }
    }];
    
    [self.schoolNameLabel setText:school.name];
    [self.imgNumButton setTitle:[NSString stringWithFormat:@"%@",@(school.num_img)] forState:UIControlStateNormal];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoSchoolImg)];
    [self.schoolImageView addGestureRecognizer:tap];
    self.schoolImageView.userInteractionEnabled = YES;
}

reveal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2022-09-08 16:32:31

你在ViewController里面实现的是tableView的代理,但是贴出来的Cell怎么是UICollectionViewCell。

看你这代码如果要出问题的话,很有可能是因为回调的block不在主线程上运行,可以尝试打断点排查,特别是-getUnreadMessageNum方法里面的block。

如果是刷新线程错了,可以用这个宏包装刷新UI的代码轻松解决:

// 确保代码运行在主线程的宏定义
#define dispatch_main_async_safe(block)\
    if ([NSThread isMainThread]) {\
        block();\
    } else {\
        dispatch_async(dispatch_get_main_queue(), block);\
    }

如果是-getUnreadMessageNum方法的问题,可以这样修正:

- (void)getUnreadMessageNum
{
    [ZXDynamicMessage getNewSchoolDynamicMessageWithUid:GLOBAL_UID sid:[ZXUtils sharedInstance].currentSchool.sid block:^(NSInteger newMessageNum, NSError *error) {
        dispatch_main_async_safe(^{
            unreadNum = newMessageNum;
            [self.tableView reloadData];
        });
    }];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文