表视图从最后一节开始:numberOfRowsInSection

时间:2016-09-13 10:12:06

标签: ios swift uitableview

这看起来可能与此重复:numberOfRowsInSection starts with last section,但所提供的解决方案并不适用于我,而且很久以前就提出了问题,而且它是在Objective-C中。 AFAIK语言与它无关。

步骤:

  1. 调用API
  2. 添加对阵列的响应 - 10个结果
  3. 重新加载表
  4. numberOfRowsInSections被召唤。
  5. {li>

    print("section: \(section)") numberOfRowsInSections中打印

    section: 9
    section: 0
    section: 1
    section: 2
    section: 3
    section: 4
    section: 5
    section: 6
    section: 7
    section: 8
    

    这是不受欢迎的行为,因为我想在每个部分中包含不同数量的行。

    由于numberOfRowsInSections决定了每个部分中的单元格数量,因此我会根据每个数组中的特定值来计算部分数量。

    响应:

    {
        count = 10;
        "post_results" =     (
                    {
                attachment = "";
                category = "An Act of Kindness";
                "comments_count" = 5;
                description = xjjxjsj;
                "last_comment" =             {
                    "comment_date" = "2016-09-13 08:24:38";
                    "comment_id" = 199;
                    description = "dddg\n";
                    "user_picture" = "/uploads/2016/09/popup-40x40.png";
                    username = testing1;
                };
                "likes_count" = 0;
                "post_date" = "2016-09-13 07:59:17";
                "post_id" = 1866;
                "share_count" = "";
                title = jdjsjsj;
                "upload_type" = 0;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = testing1;
            },
                    {
                attachment = "";
                category = "In The News";
                "comments_count" = 6;
                description = "check timing";
                "last_comment" =             {
                    "comment_date" = "2016-09-13 10:05:11";
                    "comment_id" = 207;
                    description = "hdhd\n";
                    "user_picture" = "/uploads/2016/09/popup-40x40.png";
                    username = testing1;
                };
                "likes_count" = 0;
                "post_date" = "2016-09-13 07:06:46";
                "post_id" = 1864;
                "share_count" = "";
                title = "new post";
                "upload_type" = 0;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = testing1;
            },
                    {
                attachment = "";
                category = "An Act of Kindness";
                "comments_count" = 2;
                description = "timer testing";
                "last_comment" =             {
                    "comment_date" = "2016-09-13 10:01:41";
                    "comment_id" = 201;
                    description = "hddn\n";
                    "user_picture" = "/uploads/2016/09/popup-40x40.png";
                    username = testing1;
                };
                "likes_count" = 0;
                "post_date" = "2016-09-13 06:56:36";
                "post_id" = 1862;
                "share_count" = "";
                title = hiii;
                "upload_type" = 0;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = testing1;
            },
                    {
                attachment = "";
                category = "An Act of Kindness";
                "comments_count" = 0;
                description = Hloo;
                "last_comment" =             {
                    "comment_date" = "";
                    "comment_id" = "";
                    description = "";
                    "user_picture" = "";
                    username = "";
                };
                "likes_count" = 0;
                "post_date" = "2016-09-13 06:54:48";
                "post_id" = 1860;
                "share_count" = "";
                title = testing;
                "upload_type" = 0;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = testing1;
            },
                    {
                attachment = "";
                category = "Making a Difference";
                "comments_count" = 0;
                description = TestContentdfdf899;
                "last_comment" =             {
                    "comment_date" = "";
                    "comment_id" = "";
                    description = "";
                    "user_picture" = "";
                    username = "";
                };
                "likes_count" = 0;
                "post_date" = "2016-09-13 06:54:14";
                "post_id" = 1858;
                "share_count" = "";
                title = TestTitleerer899;
                "upload_type" = 0;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = max57;
            },
                    {
                attachment = "/uploads/2016/09/Koala6-440x330.jpg";
                category = "Making a Difference";
                "comments_count" = 0;
                description = TestContentdfdf89;
                "last_comment" =             {
                    "comment_date" = "";
                    "comment_id" = "";
                    description = "";
                    "user_picture" = "";
                    username = "";
                };
                "likes_count" = 0;
                "post_date" = "2016-09-13 06:49:07";
                "post_id" = 1855;
                "share_count" = "";
                title = TestTitleerer89;
                "upload_type" = 1;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = max57;
            },
                    {
                attachment = "www.praizbuk.com";
                category = "An Act of Kindness";
                "comments_count" = 0;
                description = "";
                "last_comment" =             {
                    "comment_date" = "2016-09-13 08:16:01";
                    "comment_id" = 194;
                    description = "jddj\n";
                    "user_picture" = "/uploads/2016/09/popup-40x40.png";
                    username = testing1;
                };
                "likes_count" = 1;
                "post_date" = "2016-09-09 07:12:44";
                "post_id" = 1846;
                "share_count" = "";
                title = db;
                "upload_type" = 3;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = kkk;
            },
                    {
                attachment = "/uploads/2016/09/dummy3.mp4";
                category = "An Act of Kindness";
                "comments_count" = 1;
                description = xbbdbd;
                "last_comment" =             {
                    "comment_date" = "2016-09-09 07:17:36";
                    "comment_id" = 192;
                    description = "uugh\n";
                    "user_picture" = "/2016/09/popup-40x40.png";
                    username = kkk;
                };
                "likes_count" = 1;
                "post_date" = "2016-09-09 06:19:20";
                "post_id" = 1842;
                "share_count" = "";
                title = zyhh;
                "upload_type" = 2;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = kkk;
            },
                    {
                attachment = "/uploads/2016/09/dummy2.mp4";
                category = "In The News";
                "comments_count" = 0;
                description = "new video uploaded";
                "last_comment" =             {
                    "comment_date" = "";
                    "comment_id" = "";
                    description = "";
                    "user_picture" = "";
                    username = "";
                };
                "likes_count" = 0;
                "post_date" = "2016-09-09 05:49:48";
                "post_id" = 1838;
                "share_count" = "";
                title = "my video";
                "upload_type" = 2;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = kkk;
            },
                    {
                attachment = "";
                category = "An Act of Kindness";
                "comments_count" = 0;
                description = Vdfdfdfdfd;
                "last_comment" =             {
                    "comment_date" = "";
                    "comment_id" = "";
                    description = "";
                    "user_picture" = "";
                    username = "";
                };
                "likes_count" = 0;
                "post_date" = "2016-09-08 12:00:24";
                "post_id" = 1836;
                "share_count" = "";
                title = fff;
                "upload_type" = 0;
                "user_image" = "/uploads/2016/09/popup-40x40.png";
                "user_name" = kkk;
            }
        );
        status = ok;
    }
    

    表视图方法是:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let post = arrPost[section]
        let comment = post.last_comment
    
        print("section: \(section)")
    
        if post.upload_type! == "0" {
            if let _ = comment!.comment_id {
                return 2
            } else {
                return 1
            }
        } else {
            if let _ = comment!.comment_id {
                return 3
            } else {
                return 2
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

响应参数类型存在一些问题。

@Avt最后的结论在评论中说:

  

如果正确实现了UITableViewDataSource / delegate,无论首先请求哪个部分,它都应该有效。