zoukankan      html  css  js  c++  java
  • UITableView加载显示更多内容

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @end

    #import "ViewController.h"

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

    {

        NSMutableArray *dataArray;//uitableview要显示数据

        NSMutableArray *moreArray;//加载更多要显示的数据

    }

    @property (weak, nonatomic) IBOutlet UITableView *myTableView;

    @end

    @implementation ViewController

    #pragma mark - life circle

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        [self addObjectToArray];

        //这里不需要设置代理,因为已经在storyboard上进行关联

        //_myTableView.dataSource=self;

        //_myTableView.delegate=self;

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    #pragma mark -UITableViewDataSource

    //返回每个setion对应的行数

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

    {

        return dataArray.count+1;

    }

    //返回section数

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 1;

    }

    //返回cell

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

    {

        static NSString *cellIdentifier=@"Cell";

        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell==nil)

        {

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

        }

        if(indexPath.row==dataArray.count)

        {

        cell.textLabel.text=@"加载更多more";

        }

        else

        {

            cell.textLabel.text=dataArray[indexPath.row];

        }

        return cell;

        

        

        

    }

    #pragma mark -UITableViewDataDelegate

    //选择行数中的每行

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

    {

        if (indexPath.row==dataArray.count)

        {

           // UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath];

            

            //loadMoreCell.textLabel.text=@"加载更多more";

            

            //运用gcd多线程

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                

                //后台线程

                [self loadMore];

                

                dispatch_async(dispatch_get_main_queue(), ^{

                   //主线程

                    [self appendTableWith:moreArray];

               });

                

            });

            

            [tableView deselectRowAtIndexPath:indexPath animated:YES];

            return;

        }

    }

    #pragma mark - private

    //重新getter方法

    -(NSMutableArray *)dataArray

    {

        if (dataArray==nil)

        {

            dataArray=[NSMutableArray array];

            

        }

        return dataArray;

    }

    //刚开始添加10条记录

    -(void)addObjectToArray

    {

        for (int i=0; i<10; i++)

        {

            [self.dataArray addObject:[NSString stringWithFormat:@"cell %i",i]];

        }

    }

    -(void)loadMore

    {

        //点击加载更多行 要追加显示的记录数

        moreArray=[NSMutableArray array];

        for (int i=0; i<10; i++)

        {

            [moreArray addObject:[NSString stringWithFormat:@"cell ++%i",i]];

        }

    }

    //往tableview里面追加数据

    -(void)appendTableWith:(NSMutableArray *)data

    {

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

        {

            //在原有数据的基础上再追加数据

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

        }

        

        //把要追加显示的数据插入到指定cell的行中去

        NSMutableArray *insertIndexPaths=[NSMutableArray array];

        for (int j=0; j<data.count; j++)

        {

            NSIndexPath *newPath=[NSIndexPath indexPathForRow:[dataArray indexOfObject:[data objectAtIndex:j]] inSection:0];

            [insertIndexPaths addObject:newPath];

        }

        [self.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];

    }

    @end

     

  • 相关阅读:
    centos7安装rabbitmq 总结
    python第六十三天-- 第十一周作业
    python第六十一天,第六十二天 redis
    python第六十天-----RabbitMQ
    python第五十四天--第十周作业
    python第五十三天--进程,协程.select.异步I/O...
    python第五十二天---第九周作业 类 Fabric 主机管理程序
    python第五十一天----线程,Event,队列
    Python基础之-面向对象编程(引言)
    Python中的模块
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4081369.html
Copyright © 2011-2022 走看看