如何从另一种方法显示 UITableViewCell 的更新值?

How to show Updated values of UITableViewCell from another method ?

我在全球范围内声明

  NSIndexPath *selectedIndexPath;

这是我的选择方法

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
  {
   UIAlertView *loAlert = [[UIAlertView alloc]initWithTitle:@"Are you sure you want to redeem?" message:nil delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
   loAlert.tag  = 10;
   [loAlert show];
   selectedIndexPath = indexPath;
  }

选择行后我显示警报

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex 
 {
      if(alertView.tag == 10)
     {
         if(buttonIndex == 0)
         {
           [self redeemOfferService];
         }
     }
 }

然后调用redeemOfferService,在这个方法中我遇到了问题

- (void)redeemOfferService
{

   OffersBoughtCellTableViewCell * selectedCell1 = [offersTableView cellForRowAtIndexPath:selectedIndexPath]; 

  NSLog(@"%@ ************",   selectedCell1.quantity.text);
   // for e.g. selected cell's quantity is 0 (here i can see selected cell quantity)
   i want to change and update here as follow  

   selectedCell1.quantity.text = @"Quantity  : 5";

  // after this i can see quantity in my cell is 5 but 
     when i scroll values are again showing from

  cellForRowAtIndexPath i want to show updated value on cell,
  how to do this ?
}

有关更多详细信息,请参见我的 cellForRowAtIndexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  OffersBoughtCellTableViewCell  *cell = (OffersBoughtCellTableViewCell *) 
  [tableView dequeueReusableCellWithIdentifier:@"offersCell" forIndexPath:indexPath];
  OfferDetail * offerDetailObj=[[OfferDetail alloc]init];

// OfferDetail, OffersBoughtDetails are NSObject class 
// @property (nonatomic, strong) OfferDetail *offerObj;& OffersBoughtDetails * selectedOfferBoughtObj;
// and @property (nonatomic, strong) NSArray *offersBought;

  offerDetailObj=[self.listOfOffers objectAtIndex:indexPath.row];
  OffersBoughtDetails * offerBoughtObj=[self.offerObj.offersBought objectAtIndex:indexPath.row];
  selectedOfferBoughtObj=offerBoughtObj;
  cell.quantity.text=[NSString stringWithFormat:@"Quantity  : %@",offerBoughtObj.quantity];

}

据我了解你在选择单元格后遇到的问题你想从 UITableView 中删除该单元格。

为此,将所有 UITable 数据放入 NSMutableArray

并在完成所有任务后在您的 - (void)redeemOfferService 方法中 只需删除所选对象,例如:

      [dataArray removeObject:@"selectedObject"];

并重新加载 table 数据

      [tableView reloadData];

问题是,当您再次滚动到更改后的单元格时,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 会被调用,但您的数据不会更改,cell.quantity.text=[NSString stringWithFormat:@"Quantity : %@",offerBoughtObj.quantity];,因此您的单元格数量仍然显示您的数据模型提供的 BoughtObj是。

由于 iOS 使用 MVC,解决方案是更改数据模型,然后将模型更改反映到视图中。

- (void)redeemOfferService {
    OffersBoughtDetails * offerBoughtObj=[self.offerObj.offersBought objectAtIndex:selectedIndexPath];
    // change offerBoughtObj to the value you want
    offerBoughtObj.quantity = whateveryoulike;
    // reload your tableview
    [tablview reloadRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

您必须更新 table 的源,在您的情况下是 offerDetailObj,因此在 - (void)redeemOfferService 方法中,从数组中获取数据并获取所选对象,更新它的值并将其再次保存到您的数组中。我认为这应该有效:

- (void)redeemOfferService
{

   OffersBoughtCellTableViewCell * selectedCell1 = [offersTableView cellForRowAtIndexPath:selectedIndexPath]; 

  NSLog(@"%@ ************",   selectedCell1.quantity.text);
   // for e.g. selected cell's quantity is 0 (here i can see selected cell quantity)
   //i want to change and update here as follow  

   selectedCell1.quantity.text = @"Quantity  : 5";

  //Update your source array to the correct value
  offerDetailObj=[self.listOfOffers objectAtIndex:indexPath.row];
  OffersBoughtDetails * offerBoughtObj=[self.offerDetailObj.offersBought objectAtIndex:indexPath.row];
  OffersBoughtDetails.quantity = 5;
  [offerDetailObj replaceObjectAtIndex:indexPath.row withObject:offerBoughtObj];
  self.listOfOffers replaceObjectAtIndex:indexPath.row withObject:offerDetailObj];

}

你的数组应该是 mutable 这样你就可以 add/remove/replace 反对它,这意味着你的 listOfOffersoffersBought 应该是 NSMutableArray

我认为在您的 alertViewDelegate 方法中,如果用户选择 NO,您应该将 selectetdIndexPath 设置为 nil

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex 
{
   if(alertView.tag == 10)
  {
     if(buttonIndex == 0)
     {
       [self redeemOfferService];
     }
      else
        selectedIndexPath = nil;
  }
}

并且在您的 redeemOffService 中您应该重新加载单元格

- (void)redeemOfferService
{

  OffersBoughtCellTableViewCell * selectedCell1 = [offersTableView cellForRowAtIndexPath:selectedIndexPath]; 

  NSLog(@"%@ ************",   selectedCell1.quantity.text);
// for e.g. selected cell's quantity is 0 (here i can see selected cell quantity)
i want to change and update here as follow  

selectedCell1.quantity.text = @"Quantity  : 5";
[self.tableView reloadRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

selectedIndexPath = nil;
}

希望对您有所帮助 :D