使用时标签显示文字不好[cell setNeedsDisplay]

时间:2014-03-11 10:13:24

标签: ios iphone objective-c uicollectionview uicollectionviewcell

我有一个UICollectionView来显示用户的用户名,当我添加新用户或修改用户时 - >我将更新到数据库 - >然后再次获取所有数据(来自数据库)。然后重新加载UICollectionView。我想要的是:如果我在索引3处修改用户,那么在重新加载之后,该用户仍然停留在索引3(如果我添加新用户,则该用户将显示在结束位置)。所以我使用 setNeedsDisplay 。但我有一个问题,当我使用setNeedsDisplay时,我的标签显示文字不好,如下所示: enter image description here

当我注释掉 [cell setNeedsDisplay]; 时,标签的文字显示得很好。但是每个用户的索引都不能正常显示。这就是我的代码

    - (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];
}

和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];
}

这里是自定义集合视图:

    @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];
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

由于此行[self.contentView addSubview:view];,它已多次添加,因为setNeedDisplay每次都会调用drawRect:。为避免这种情况,请尝试以下..

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