iOS UITableView-Insert

#import "YoungViewController.h"


@interface YoungViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    NSMutableArray * items; //定义开始加载的数组

}


@end


@implementation YoungViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    items = [[NSMutableArray alloc]init]; //初始化

#pragram mark  赋值

    [items addObject:@"AA"];

    [items addObject:@"BB"];

    [items addObject:@"CC"];

    [items addObject:@"DD"];

    [items addObject:@"EE"];

    [items addObject:@"FF"];

    [items addObject:@"GG"];

    [items addObject:@"HH"];

    

    UITableView * tableView = [[UITableView alloc]init];

    tableView.frame = CGRectMake(0, 20, 320, 400);

    [tableView setBackgroundView:nil];

    [tableView setBackgroundColor:[UIColor grayColor]];

    [tableView setDelegate:self];

    [tableView setDataSource:self];

    [tableView setTag:1000]; //1-100是苹果公司预留tag值

    [self.view insertSubview:tableView atIndex:0];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    int count = [items count];

    return count+1;

}

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

{

    

    static NSString* AllActivitiesCellIdentifier = @"AllActivitiesCellIdentifier";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:AllActivitiesCellIdentifier];

    if(cell==nil){

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AllActivitiesCellIdentifier];

    }

    if(indexPath.row<[items count])

    {

        cell.textLabel.text = [items objectAtIndex:indexPath.row];

    }

    else

    {

        cell.textLabel.text = @"Loading More";

    }

    return cell;

}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if([indexPath row] == [items count])

    {

        [self performSelectorInBackground:@selector(LoadMore) withObject:nil];

    }

}

-(void)LoadMore

{

    NSMutableArray *more = [[NSMutableArray alloc]init];

    

    [more addObject:@"11"];

    [more addObject:@"22"];

    [more addObject:@"33"];

    

    [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];

}

-(void)appendTableWith:(NSMutableArray*)data

{

    for(int i=0;i<[data count];i++)

    {

        [items addObject:[data objectAtIndex:i]];

    }

    

    NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:100];

    for(int ind =0;ind<[data count];ind++)

    {

        NSIndexPath  *newPath =  [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];

        

        [insertIndexPaths addObject:newPath];

    }

    UITableView * tas = (UITableView*)[self.view viewWithTag:1000];

    [tas insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}


@end


你可能感兴趣的:(ios,UITableView)