美文网首页
iOS UITableViewCell里面for循环创建N个UI

iOS UITableViewCell里面for循环创建N个UI

作者: ZH0303 | 来源:发表于2019-09-30 17:19 被阅读0次

        记录一下今天遇到的一个问题:

    数据源是一个这样子结构的数据

    [

        {

            "key" = [

                    {

                    },

                    {

                    }

                ]

        },

        {

            "key" = [

                    {

                    },

                    {

                    }

            ]

        }

    ],很明显能看出这个self.dataArray里面是两个字典,每个字典通过取key值[@"key"]得到的是第二层数组,第二层数组里面通过for循环遍历得到的又是N个字典。

    需求是:

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

    {

        return self.dataArray.count > 0 ? self.dataArray.count : 0;

    }

    cellForRow方法里面:

    if([[self.dataArray[indexPath.row]objectForKey:@"key"]count] >0) {

        for(inti =0; i < [[self.dataArray[indexPath.row]objectForKey:@"key"]count]; i ++) {

                UITextField*textField = [[UITextFieldalloc]initWithFrame:CGRectMake(lbl3.x+lbl3.width+10, lbl3.y, bgView2.width-10-48-48-10-(lbl3.x+lbl3.width+10), lbl3.height)];

                ...

                [textFieldsetTag:1000000*indexPath.row+i]; // 这里在给textField设置tag值的时候要这样子设置就可以取到标题所提到的row和下标了,至于为什么,接着往下分析:首先为什么要用1000000(你也可以自己定义,最好越大越好)这么大的值乘indexPath.row呢?因为如果用一个很小的值乘的话,假如[self.dataArray[indexPath.row]objectForKey:@"key"]count]比定义的值还大,那么接下来计算row和下标的方法将不可用(换句话说就是除非self.dataArray[indexPath.row]objectForKey:@"key"]里面的元素数量比1000000还大才会出问题),所以要定义一个很大的值。

        }

    }

    - (void)textFieldDidEndEditing:(UITextField*)textField

    {

        // 就是这么简单:

        NSInteger row = textField.tag/1000000;

        int i = textField.tag%1000000;

        现在知道为什么上面要定义1000000那么大的数了吧。

    }

    相关文章

      网友评论

          本文标题:iOS UITableViewCell里面for循环创建N个UI

          本文链接:https://www.haomeiwen.com/subject/njlkyctx.html