iOS学习笔记--tableView中如何获取cell上textfiled的值

来源:互联网 发布:音画制作软件 编辑:程序博客网 时间:2024/06/15 13:57
最近在项目中遇到了这样一个问题,在tableView的cell上添加textfiled,然后获取cell上textfiled的值。cell的个数是可以动态改变的。如下图:

这里写图片描述

在网上看了很多别人写的没找见容易点的实现方法,自己写了一个比较笨的方法。主要代码如下:
@property(nonatomic,strong)NSMutableArray * arrray//数组中元素的个数为cell的行数@property(nonatomic,strong)NSMutableArray * tmparrray;//存cell@property(nonatomic,strong)NSMutableArray * tmpIndexPath;//存indexPath@property(nonatomic,strong)NSMutableArray * tmparrayaaaaa;//获取cell的数据
在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中加入 [self.tmpIndexPath addObject:indexPath];   [self.tmparrray addObject:cell];

定义一个按钮,当点击按钮的时候获取cell上textfiled中所填的值。

for(int i = 0; i<self.tmpIndexPath.count; i++){     [self.tmparrayaaaaa removeObject:[NSString stringWithFormat:@"%id",i]];     cell=  [self.tmpTableView cellForRowAtIndexPath:[self.tmpIndexPath objectAtIndex:i]];     [self.tmparrayaaaaa addObject:cell.tmpTextfiled.text];  for (int i = 0; i < ((self.arrray.count * (self.arrray.count +1)) /2 ) - self.arrray.count; i++)    {        [self.tmparrayaaaaa removeObjectAtIndex:0];    }    NSLog(@"======获取cell的值====>%@",self.tmparrayaaaaa);
主要思路:在点击取值按钮后根据indexpath来遍历cell ,然后将cell中textfiled得值存到tmparrayaaaaa数组中。因为i每次循环,textfiled的值毒药添加到数组中,所以数组终会有重复值,此时tmparrayaaaaa数组中元素的个数为(self.arrray.count * (self.arrray.count +1)) /2 ,故用循环的方式一次移除多余的元素。

demo 地址
链接: http://pan.baidu.com/s/1dE9dZvb 密码: jhsa

原创粉丝点击