简体   繁体   中英

tableview add custom tableview cell more than 500 then working slow?

I have made UITableView in obj-c for iOS and my array contains more then 500 elements. Tableview cell is editable, insert row within cell, open textview with keyboard and picker-view then app become slow.my CPU memory usage also reached to 100%.

I have to manage my app CPU memory usage.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CGFloat TableCellHeight = 0.000;
    for (int i=0; i<arrQuestions.count; i++) {
        templateRow *object = [arrQuestions objectAtIndex:i];
        CGFloat height2= object.rowHeight ;
        TableCellHeight = TableCellHeight+height2;
    }
    tvQuestion.frame=CGRectMake(tvQuestion.frame.origin.x, tvQuestion.frame.origin.y, tvQuestion.frame.size.width, TableCellHeight+footer.view.frame.size.height+footer.view.frame.size.height);
    height=tvQuestion.frame.origin.y+tvQuestion.frame.size.height;
    scrollView.contentSize=CGSizeMake(320,height);
    return arrQuestions.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    templateRow *object = [arrQuestions objectAtIndex:indexPath.row];
    CGFloat height= object.rowHeight ;
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"customCell";
    templateRow *object = [arrQuestions objectAtIndex:indexPath.row];
    customeCellAnswer *cell = (customeCellAnswer *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray * topLevelObjects;
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            topLevelObjects  = [[NSBundle mainBundle] loadNibNamed:@"customeCellAnswer" owner:self options:nil];
        }
        else {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"customeCellAnswer_ipad" owner:self options:nil];
        }
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell = (customeCellAnswer *)currentObject;
                NSLog(@"CustomCell:%@",currentObject);

                if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
                {
                }
                else {
                    [cell setFrame:CGRectMake(0,0,750,100)];
                }

                break;
            }
        }
    }

    if (object.rowType==10){
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    cell.delegate=self;
    [cell setDataRow:object];
    [cell configureCell];

    return cell;
}

What you are doing is absolutely bizarre. numberOfRowsInSection shouldn't have any side effects, like changing a frame size. Typically, your code for numberOfRowsInSection should be the one line

return arrQuestions.count;

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