UITableView reloadData从单元格中丢失元素

时间:2014-10-23 15:01:35

标签: objective-c uitableview ios8 reloaddata

我有一个UITableView,他的数据源是我的界面中声明的NSMutableArray:

@property (strong, nonatomic) NSMutableArray *arrElements;

现在我实施了一个简单的#34;加载更多"这样:

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (endScrolling >= scrollView.contentSize.height)
    {
        //only if i have others results i will call load more
        if([self.items_amount integerValue] > self.arrElements.count) {

            if(DEBUG_MODE == 1) {
                NSLog(@"Load more");
            }

            //get start param from array size and call load more
            NSNumber *start = [NSNumber numberWithInteger:([self.arrElements count]+1)];
            NSArray *passParams = [NSArray arrayWithObjects:self.menuItem,start,[NSNumber numberWithInteger:NUM_RESULTS_FOR_PAGE], nil];
            [self performSelector:@selector(loadMore:) withObject:passParams afterDelay:0.1];

        }
    }
}

这是我的loadMore方法:

//load more elements
- (void)loadMore:(NSArray *)arrParams {

    //(MenuItem *)menuItem startingFrom:(NSNumber *)start numResults:(NSNumber *)results;

    MenuItem *menuItem  = [arrParams objectAtIndex:0];
    NSNumber *start     = [arrParams objectAtIndex:1];
    NSNumber *results    = [arrParams objectAtIndex:2];

    if(DEBUG_MODE == 1) {
        NSLog(@"Before load more %lu", (unsigned long)[self.arrElements count]);
    }

    //call API and parse it
    WebServicesClient *restClient = [[WebServicesClient alloc] init];
    NSData *data = [restClient callWorkAPI:[menuItem pathAPI] inLanguage:[[NSLocale preferredLanguages] objectAtIndex:0] withLat:self.latitude withLng:self.longitude startingFrom:start numRows:results];
    for(Work* work in [[restClient parseWorks:data] objectForKey:@"items"]) {
        [self.arrElements addObject:work];
    }

    if(DEBUG_MODE == 1) {
        NSLog(@"After load more %lu", (unsigned long)[self.arrElements count]);
    }

    [self.tableElem reloadData];

}

这是我的自定义单元格:

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

        static NSString *CellIdentifier;
        CellIdentifier = @"HomeCell";

        HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(!cell) {
            cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        Work *work = (Work *)[[self arrElements] objectAtIndex:indexPath.row];

        cell.backgroundColor = [UIColor clearColor];
        cell.labelTitleItem.text = [work title];
        cell.labelSubtitleItem.text = @"my category"


        //if exists image
        if([[work image] isKindOfClass:[NSString class]] && [[work image] length] > 0) {

            dispatch_async(dispatch_get_global_queue(0,0), ^{
                NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[work image]]];
                if ( data == nil )
                    return;
                dispatch_async(dispatch_get_main_queue(), ^{
                    cell.imageItem.image = [UIImage imageWithData: data];
                });
            });

        } else {
            //remove image setting height to 0
            [cell.imageItem setFrame:CGRectMake(cell.imageItem.frame.origin.x, cell.imageItem.frame.origin.y, cell.imageItem.frame.size.width, 0)];
        }


        if([[work distance] isKindOfClass:[NSString class]]) {
            [cell.imagePriority setBackgroundColor:[UIColor lightGrayColor]];
            cell.labelDistanceItem.text = [self convertDistance:[work distance]];
            cell.labelDistanceItem.textColor = [UIColor whiteColor];
        } else {
            //remove image setting height to 0
            [cell.imagePriority setFrame:CGRectMake(cell.imagePriority.frame.origin.x, cell.imagePriority.frame.origin.y, cell.imagePriority.frame.size.width, 0)];
            //remove label distance setting height to 0
            [cell.labelSubtitleItem setFrame:CGRectMake(cell.labelSubtitleItem.frame.origin.x, cell.labelSubtitleItem.frame.origin.y, cell.labelSubtitleItem.frame.size.width, 0)];
        }

        return cell;

    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self arrElements] count];
}

现在问题是在reloadData之后我丢失了我的CustomCell的UILabel(我的类别)的文本 拜托,有人建议吗?

1 个答案:

答案 0 :(得分:0)

我和TalbeViews有类似的问题。尝试使用方法

- (void) tableView:(UITableView *)tableView willDisplayCell:(RCGameTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

添加所需的元素。保持你在cellForRowAtIndexPath开头做的单元初始化,但是之后立即返回单元格,并在我提到的willDisplayCell委托函数中设置背景颜色,标签,图像等。