表格视图从最后一部分开始:numberOfRowsInSection [英] Table View starts with last section in: numberOfRowsInSection

查看:31
本文介绍了表格视图从最后一部分开始:numberOfRowsInSection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来与此重复:numberOfRowsInSection 从最后一部分开始,但是提供的解决方案对我不起作用,很久以前就有人问过它,它是在 Objective-C 中的.AFAIK 语言与此无关.

This may look duplicate of this: numberOfRowsInSection starts with last section, but the solution provided didn't work from me and it was asked long way back and it was in Objective-C. AFAIK language has nothing to do with it.

步骤:

  1. 调用 API
  2. 向数组添加响应 - 10 个结果
  3. 重新加载表
  4. numberOfRowsInSections 被调用.
  5. print("section: \(section)") in numberOfRowsInSections 打印

  1. Call API
  2. Add response to array - 10 results
  3. Reload Table
  4. numberOfRowsInSections gets called.
  5. print("section: \(section)") in numberOfRowsInSections prints

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

这是不受欢迎的行为,因为我希望每个部分中的行数不同.

It is undesired behaviour since I want to have different number of rows within each section.

由于 numberOfRowsInSections 决定了每个部分中的单元格数量,所以我根据每个数组中的某些值来计算部分的数量.

Since numberOfRowsInSections decides the number of cell within each section, I count the number of section based upon the certain values in each array.

回复:

{
    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;
}

而Table View方法是:

And Table View method is:

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
        }
    }

}

推荐答案

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

There were some issues with the response parameters type.

@Avt 在评论中所说的最终结论:

Final Conclusion as @Avt said in comments:

如果正确实现了 UITableViewDataSource/delegate,无论首先请求哪个部分,它都应该可以工作.

这篇关于表格视图从最后一部分开始:numberOfRowsInSection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆