简体   繁体   中英

label display text not well when use [cell setNeedsDisplay]

I have a UICollectionView to display username of user, and when I add new or modify a user -> I will update into database -> then get all data again(from the data base). and then reload UICollectionView. All I want is: if I modify a user at index 3 then after reload this user is still stay at index 3(and if I add a new user this user will display at the end position). So that I use setNeedsDisplay . But I have a problem that my label display text not well when I use setNeedsDisplay,as below: 在此处输入图片说明

when I comment out [cell setNeedsDisplay]; then the text of label is display well. But the index of each user is not display right as I want.Here is my code

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if(!dbManager)
        dbManager = [DBManager sharedInstant];
    UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAdd)];
    self.navigationItem.rightBarButtonItem = btnAdd;

    UIBarButtonItem *btnFilter = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showFilter)];
    self.navigationItem.leftBarButtonItem = btnFilter;
    [[DBManager sharedInstant] setDelegate:self];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
        [self.collectionView registerClass:[UserCollectionItemView class] forCellWithReuseIdentifier:@"UserCollectionItemView"];
        cells = [[NSMutableArray alloc] init];
    }
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!dbManager.synchronized) {
        [datasource removeAllObjects];
        datasource = nil;
        if (contactType == ContactTypeCustomer)
            [dbManager requestData:kDbCustomers predicate:nil target:self];
        else if (contactType == ContactTypeSuppplier)
            [dbManager requestData:kDbSuppliers predicate:nil target:self];
    }
    [self setLayout];
}

and for collectionview:

#pragma mark
#pragma UICollectionDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)cv numberOfItemsInSection:(NSInteger)section
{
    return [datasource count];
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    [cells addObject:cell];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UserCollectionItemView *cell;

//    if([cells count])
//    {
//        cell = [cells lastObject];
//        [cells removeLastObject];
//    }
//    else
        cell = [cv dequeueReusableCellWithReuseIdentifier:@"UserCollectionItemView" forIndexPath:indexPath];
    if (contactType == ContactTypeCustomer) {
        POSCustomer *customer = [datasource objectAtIndex:indexPath.item];
        cell.displayname = customer.CompanyName;
    }
    else if (contactType == ContactTypeSuppplier){
        POSSupplier *supplier = [datasource objectAtIndex:indexPath.item];
        cell.displayname = supplier.CompanyName;
    }
    cell.backgroundColor = [UIColor clearColor];
    [cell setNeedsDisplay];
    return cell;
}

- (void)collectionView:(UICollectionView *)cv didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    dbManager.synchronized = YES;
    if (contactType == ContactTypeCustomer) {
        POSCustomer *customers = [datasource objectAtIndex:indexPath.item];
        [self showEditCustomer:customers];
    }
    else if (contactType == ContactTypeSuppplier){
        POSSupplier *suppliers = [datasource objectAtIndex:indexPath.item];
        [self showEditSupplier:suppliers];
    }

}

-(void)showEditCustomer:(POSCustomer *)customer{
    ContactFormViewController *form = [[ContactFormViewController alloc] initWithNibName:@"ContactFormViewController" bundle:nil];
    [form setContactType:ContactTypeCustomer];
    form.posCustomer = customer;
    [self.navigationController pushViewController:form animated:YES];
}

-(void)showEditSupplier:(POSSupplier *)supplier{
    ContactFormViewController *form = [[ContactFormViewController alloc] initWithNibName:@"ContactFormViewController" bundle:nil];
    [form setContactType:ContactTypeSuppplier];
    form.posSupplier = supplier;
    [self.navigationController pushViewController:form animated:YES];
}

#pragma mark
#pragma DBDelegate

- (void)requestDataCompleted:(NSMutableArray *)results
{
    datasource = results;
    [self.collectionView reloadData];
}

and here is for custom collectionview:

    @synthesize displayname;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    CGRect frame = self.contentView.frame;
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.layer.borderWidth = 0.5;
    [view.layer setBorderColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.2 alpha:1].CGColor];
    view.layer.cornerRadius = 5;
    [view setBackgroundColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.2 alpha:0.3]];

    UIImageView *avatarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, frame.size.width, rect.size.width)];
    [avatarView setBackgroundColor:[UIColor clearColor]];
    [avatarView setImage:[UIImage imageNamed:@"users_icon"]];
    [view addSubview:avatarView];

    UILabel *displayName = [[UILabel alloc] initWithFrame:CGRectMake(3, frame.size.width - 10, rect.size.width - 6, 50)];
    displayName.numberOfLines = 2;
    displayName.text = displayname;
    [displayName setFont:[UIFont fontWithName:@"Arial" size:12]];
    displayName.textAlignment = NSTextAlignmentCenter;
    [displayName setTextColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.2 alpha:1]];
    [view addSubview:displayName];
    [self.contentView addSubview:view];
}

Thanks for any helps.

Because of this line [self.contentView addSubview:view]; , It've added multiple times because setNeedDisplay will call drawRect: every time. To avoid this, try below..

UIView *view = [[UIView alloc] initWithFrame:frame];
view.tag = SomeTagValue;
.
.
.
.
UIView *preView = [self.contentView viewWithTag:SomeTagValue];
[preView removeFromSuperview];
[self.contentView addSubview:view];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM