通过调度异步设置自定义单元属性时,uitableview滚动上的闪烁问题

时间:2014-03-11 05:52:58

标签: iphone ipad uitableview

我有自定义单元格的uitableview.Custom单元格包含一些字段,如label.I使用dispatch_async从耗时的过程中获取数据,然后更新main中的单元格属性。但是我在滚动时遇到自定义单元格数据的闪烁。我有:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

OpenOrdersCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIndetifier"];
LanguageModule *langModule=[LanguageModule sharedModule];

if (!cell) {
    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"OpenOrdersCell" bundle:nil];
    cell = (OpenOrdersCell *)controller.view;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.clipsToBounds = YES;
    [cell setBackgroundColor:[UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f]];
}

Order *order = [self.arrayOfOpenOrders objectAtIndex:indexPath.row];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
               {
                   NSString *str = [langModule getLocalizedString:@"Select"];

                   dispatch_async(dispatch_get_main_queue(), ^

                                  {
                                      [cell.detailButton setTitle:str forState:UIControlStateNormal];
                                      cell.detailButton.tag = indexPath.row + 1;
                                      [cell.detailButton addTarget:self action:@selector(detailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

                                  });

               });

if ([order.mStatus intValue] == 0)//called from closed order class
{
//        [cell.sendInvoiceButton setTitle:[langModule getLocalizedString:@"Reorder"] forState:UIControlStateNormal];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
    {
        NSString *str = [langModule getLocalizedString:@"Send Receipt"];
                dispatch_async(dispatch_get_main_queue(), ^

        {
            [cell.sendInvoiceButton setTitle:str forState:UIControlStateNormal];
            cell.sendInvoiceButton.tag = indexPath.row + 1;
            [cell.sendInvoiceButton addTarget:self action:@selector(sendInvoiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

            CGFloat x = cell.detailButton.frame.origin.x;//original x
            if ([Utils getSendInvoiceOption] == FALSE) {

                [cell.sendInvoiceButton setHidden:YES];
                cell.detailButton.frame = CGRectMake(cell.sendInvoiceButton.frame.origin.x+30, cell.detailButton.frame.origin.y, cell.detailButton.frame.size.width, cell.detailButton.frame.size.height);
            }
            else
            {
                cell.detailButton.frame = CGRectMake(x, cell.detailButton.frame.origin.y, cell.detailButton.frame.size.width, cell.detailButton.frame.size.height);
                [cell.sendInvoiceButton setHidden:NO];
            }
        });

    });



}





dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
{
    NSString *str = [langModule getLocalizedString:[NSString stringWithFormat:@"%@",order.mDateTime]];
    dispatch_async(dispatch_get_main_queue(), ^

                   {
                       cell.dateTimeLabel.text = str;
                   });

});



dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
{
    NSString *str = [langModule getLocalizedString:[NSString stringWithFormat:@"%@",order.mOrderId]];
    dispatch_async(dispatch_get_main_queue(), ^

                   {
                       cell.orderIdLabel.text = str;

                   });

});

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
               {
                   NSString *str = [langModule getLocalizedString:order.mTotalItems];
                   dispatch_async(dispatch_get_main_queue(), ^

                    {
                        cell.totalItemsLabel.text = str;

                    });

               });



dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
{
    NSString *str = [Utils currencyWithSymbol:[NSString stringWithFormat:@"%.02f", [order.mTotalAmount floatValue]]];
    dispatch_async(dispatch_get_main_queue(), ^

                   {
                       cell.totalAmountLabel.text  = str;
                   });

});




dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^
{
    dispatch_async(dispatch_get_main_queue(), ^

                   {

                   });

});





if (IndexPath && IndexPath.row == indexPath.row)
{
    [cell.contentView addSubview:expandedCellView];
}

cell.contentView.tag = indexPath.row + 1;

return cell;
}

如果有人可以建议我,如何解决这个问题。

0 个答案:

没有答案