TextField第一个单元格结果显示在Tableview单元格的其他单元格中

时间:2015-07-04 05:35:37

标签: ios uitableview uitextfield

我正在开发一个IOS应用程序。问题是在表视图中使用文本字段。在tableviewCell的textfield第一个单元格中输入的值,结果显示在第二个或第三个单元格中。有时,当tableview只有一行时,结果显示正确。非常感谢任何帮助或建议。

代码:

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

   return _fetchedobj.count;
}

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *MyIdentifier = @"MyReuseIdentifier";
   UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:MyIdentifier];
   if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
   }
   data = [_fetchedobj objectAtIndex:indexPath.row];

   _quantity = [[UITextField alloc] initWithFrame:CGRectMake(5.0f,28.0f,50.0f , 30.0f)];
   [_quantity setFont:[UIFont systemFontOfSize:14.0f]];
   [_quantity setTextAlignment:NSTextAlignmentCenter];
   _quantity.backgroundColor = [UIColor whiteColor];
   [_quantity setAdjustsFontSizeToFitWidth:YES];
   [_quantity addTarget:self action:@selector(changedvalue:) forControlEvents:UIControlEventAllEditingEvents];
   NSString *q_value = [data valueForKey:@"quantity"];
   NSInteger q_value1 = [q_value integerValue];
   [_quantity setText:[NSString stringWithFormat:@"%ld",(long)q_value1]];
   [cell addSubview:_quantity];

更改价值方法

- (void) changedvalue: (UITextField *)textfield
{

    NSString *text = [textfield text];
    NSInteger text1 = [text integerValue];

    NSString *changevalue =  [dict objectForKey:@"_max_purchases_per_user"];
    NSInteger valueChange =  [changevalue integerValue];
    NSString *userPurchase = [dict objectForKey:@"_max_purchases"];
    NSInteger itemPurchase = [userPurchase integerValue];

    NSString *amount =  [data valueForKey:@"amount"];
    NSInteger amount1 = [amount integerValue];

    NSInteger minValue=MIN(valueChange, itemPurchase);
    if (text1 > minValue) {
        [_quantity setText:[NSString stringWithFormat:@"%ld",(long)minValue]];
        [_total1 setText:[NSString stringWithFormat:@"%ld",minValue * amount1]];
    }else{
   [_total1 setText:[NSString stringWithFormat:@"%ld",text1 * amount1]];
    }
  integerValue] * [aNum integerValue])]];
}

3 个答案:

答案 0 :(得分:1)

您需要了解细胞重用的概念。你可以采取一些措施来解决这个问题。

首先是您的cellForRowAtIndexPath:方法。每次调用该方法时,即使已经有一个UITextField,也会放置一个新的UITextField。此外,_quantity似乎是viewController的一个实例变量,这不是一个好习惯。

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

   return _fetchedobj.count;
}

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *MyIdentifier = @"MyReuseIdentifier";
   UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:MyIdentifier];

   UITextField *quantity;

   if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];

      //This is the only place a new UITextField is needed.


       quantity = [[UITextField alloc] initWithFrame:CGRectMake(5.0f,28.0f,50.0f , 30.0f)];
       [quantity setFont:[UIFont systemFontOfSize:14.0f]];
       [quantity setTextAlignment:NSTextAlignmentCenter];
       quantity.backgroundColor = [UIColor whiteColor];
       [quantity setAdjustsFontSizeToFitWidth:YES];
       [quantity addTarget:self action:@selector(changedvalue:) forControlEvents:UIControlEventAllEditingEvents];
       [quantity setTag: 1];
       [cell addSubview:quantity];

   }
   else
   {
       quantity = [cell viewWithTag: 1];
   }

   data = [_fetchedobj objectAtIndex:indexPath.row];
   NSString *q_value = [data valueForKey:@"quantity"];
   NSInteger q_value1 = [q_value integerValue];
   [quantity setText:[NSString stringWithFormat:@"%ld",(long)q_value1]];
}

其次,在changedValue:方法中,您没有将更新的值存储在cellForRowAtIndexPath:方法中要更新的任何位置。此外,您需要一种方法来跟踪textField属于哪个单元格。

- (void) changedvalue: (UITextField *)textfield
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell: textfield.superview ];    

    NSString *text = [textfield text];
    NSInteger text1 = [text integerValue];

    NSString *changevalue =  [dict objectForKey:@"_max_purchases_per_user"];
    NSInteger valueChange =  [changevalue integerValue];
    NSString *userPurchase = [dict objectForKey:@"_max_purchases"];
    NSInteger itemPurchase = [userPurchase integerValue];

    NSString *amount =  [data valueForKey:@"amount"];
    NSInteger amount1 = [amount integerValue];

    NSInteger minValue=MIN(valueChange, itemPurchase);
    if (text1 > minValue) {
        [textField setText:[NSString stringWithFormat:@"%ld",(long)minValue]];
        [_total1 setText:[NSString stringWithFormat:@"%ld",minValue * amount1]];
    }else{
        [_total1 setText:[NSString stringWithFormat:@"%ld",text1 * amount1]];
    }

  // integerValue] * [aNum integerValue])]]; This line is incomplete

   //Update the new value in the array.
   NSMutableArray *mData = [data mutableCopy];
   NSMutableDictionary *mdic = [data objectAtIndex: indexPath.row].mutableCopy;
   [mDic setObject:textField.text];
   [mData replaceObjectAtIndex:indexPath.row withObject:mDic];
   data = mData;
}

答案 1 :(得分:1)

首先,永远不建议您将子视图直接添加到单元格。如果你想要一些customzied创建一个UITableViewCell的子类,然后将你的UITextField作为子视图添加到单元格的contentView而不是直接添加到视图。

假设您的自定义单元格是包含textField的MyCustomCell。

然后你的cellForRowAtIndexPath:应该看起来像 -

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    MyCustomCell *cell = [myTable dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    }

    // All the rendering of quantity should be done in MyCustomCell and quantity should be added as subview to its contentView. And you should also implement prepareForReuse method in your custom cell.

    data = [_fetchedobj objectAtIndex:indexPath.row];
    NSString *q_value = [data valueForKey:@"quantity"];
    NSInteger q_value1 = [q_value integerValue];
    // here you should just assign the data to the cell and your view controller should know nothing about the rendering of cell.
    cell.quantity.text = [NSString stringWithFormat:@"%ld",(long)q_value1];
}

答案 2 :(得分:-1)

您应该在tableView:willDisplayCell:forRowAtIndexPath方法而不是cellForRowAtIndexPath中设置表视图单元格的内容,因为Cocoa将尝试使用当前单元格的最后创建的单元格(可以缓存)。

相关问题