1
   | 最近在项目中遇到了这样一个问题,在tableView的cell上添加textfiled,然后获取cell上textfiled的值。cell的个数是可以动态改变的。如下图:
   | 
 

1
   | 在网上看了很多别人写的没找见容易点的实现方法,自己写了一个比较笨的方法。主要代码如下:
   | 
 
1 2 3 4 5 6 7
   | @property(nonatomic,strong)NSMutableArray * arrray//数组中元素的个数为cell的行数
  @property(nonatomic,strong)NSMutableArray * tmparrray;//存cell
  @property(nonatomic,strong)NSMutableArray * tmpIndexPath;//存indexPath
  @property(nonatomic,strong)NSMutableArray * tmparrayaaaaa;//获取cell的数据
   | 
 
1 2 3 4
   | 在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中加入
   [self.tmpIndexPath addObject:indexPath];    [self.tmparrray addObject:cell];
   | 
 
定义一个按钮,当点击按钮的时候获取cell上textfiled中所填的值。
1 2 3 4 5 6 7 8 9 10 11 12
   | 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);
   | 
 
1
   | 主要思路:在点击取值按钮后根据indexpath来遍历cell ,然后将cell中textfiled得值存到tmparrayaaaaa数组中。因为i每次循环,textfiled的值都要添加到数组中,所以数组终会有重复值,此时tmparrayaaaaa数组中元素的个数为(self.arrray.count * (self.arrray.count +1)) /2 ,故用循环的方式一次移除多余的元素。
   |