<i id='xDxyH'><tr id='xDxyH'><dt id='xDxyH'><q id='xDxyH'><span id='xDxyH'><b id='xDxyH'><form id='xDxyH'><ins id='xDxyH'></ins><ul id='xDxyH'></ul><sub id='xDxyH'></sub></form><legend id='xDxyH'></legend><bdo id='xDxyH'><pre id='xDxyH'><center id='xDxyH'></center></pre></bdo></b><th id='xDxyH'></th></span></q></dt></tr></i><div id='xDxyH'><tfoot id='xDxyH'></tfoot><dl id='xDxyH'><fieldset id='xDxyH'></fieldset></dl></div>
    • <bdo id='xDxyH'></bdo><ul id='xDxyH'></ul>
    <legend id='xDxyH'><style id='xDxyH'><dir id='xDxyH'><q id='xDxyH'></q></dir></style></legend>
  • <small id='xDxyH'></small><noframes id='xDxyH'>

        <tfoot id='xDxyH'></tfoot>
      1. 访问 cellForRowAtIndexPath 之外的自定义单元格属性

        时间:2023-07-08
          <bdo id='9DSeW'></bdo><ul id='9DSeW'></ul>

          <small id='9DSeW'></small><noframes id='9DSeW'>

            <tbody id='9DSeW'></tbody>

        • <i id='9DSeW'><tr id='9DSeW'><dt id='9DSeW'><q id='9DSeW'><span id='9DSeW'><b id='9DSeW'><form id='9DSeW'><ins id='9DSeW'></ins><ul id='9DSeW'></ul><sub id='9DSeW'></sub></form><legend id='9DSeW'></legend><bdo id='9DSeW'><pre id='9DSeW'><center id='9DSeW'></center></pre></bdo></b><th id='9DSeW'></th></span></q></dt></tr></i><div id='9DSeW'><tfoot id='9DSeW'></tfoot><dl id='9DSeW'><fieldset id='9DSeW'></fieldset></dl></div>
          <tfoot id='9DSeW'></tfoot><legend id='9DSeW'><style id='9DSeW'><dir id='9DSeW'><q id='9DSeW'></q></dir></style></legend>

                  本文介绍了访问 cellForRowAtIndexPath 之外的自定义单元格属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想要一些如下图所示的东西.用户可以单击 + 或 - 按钮,增加或减少的计数显示在 UILabel 中,即下图中的 1.
                  我知道如何实现自定义表格视图单元格.我也知道如何实现选择器.但我需要根据单击的按钮设置标签文本,这只是问题所在.如何更新单击的按钮设置自定义单元格的UILabel文本 在cellForRowAtIndexPathwith之外添加减法功能.

                  I want some thing like image below. User can click on + or - button and the increased or decreased count is display in UILabel i.e 1 in the image below.
                  I know how to implement custom table view cell .I know how to implement the selector too. but I need to set the label text according as the button clicked thats only is the problem . How can I update button clicked and set text of UILabel of custom cell outside from cellForRowAtIndexPathwith add subtract features.

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                  
                      //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                  
                       static NSString *simpleTableIdentifier = @"ProductListVIICell";
                  
                       ProductListVIICell *cell = (ProductListVIICell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
                  
                      if (cell == nil)
                      {
                           NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductListVIICell" owner:self options:nil];
                           cell = [nib objectAtIndex:0];
                       } ....................................
                          cell.btnMinus.tag = indexPath.row;
                          cell.btnPlus.tag = indexPath.row;
                  
                          [cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
                          [cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
                  
                          cell.lblCount.tag = [[NSString stringWithFormat:@"%d%d",addBtnClickCount,addBtnClickCount] integerValue];
                  
                  
                          return cell;
                      }
                  

                  我需要在这里做一些事情,但没有得到我想要的结果.cell.lblTitle.text 不工作

                  I need to do things here but didnt get what I want to acheive. cell.lblTitle.text isnt working

                    #pragma mark - UIButton selector
                  
                  -(void)addItem:(UIButton*)button {
                      itemCount++;
                      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:1];
                      ProductListVIICell *cell = (ProductListVIICell *)[self.tableView cellForRowAtIndexPath:indexPath];
                        cell.lblCount.textColor = [UIColor redColor];
                       cell.lblTitle.text = [NSString stringWithFormat:@"%d",itemCount];
                  
                      NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
                      NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
                      [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
                       NSLog(@"%d",itemCount);
                      [self.tableView reloadData];
                  
                  
                  }
                  
                  -(void)deleteItem:(UIButton*)button {
                      itemCount--;
                       NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:1];
                      ProductListVIICell *cell = (ProductListVIICell *)[self.tableView cellForRowAtIndexPath:indexPath];
                  
                      cell.lblCount.textColor = [UIColor redColor];
                      cell.lblTitle.text = [NSString stringWithFormat:@"%d",itemCount];
                  
                      NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
                      NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
                      [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
                       NSLog(@"%d",itemCount);
                      [self.tableView reloadData];
                  
                  }
                  

                  这些链接也很有用

                  访问 cellForRowAtIndexPath 之外的单元格属性

                  http://code.tutsplus.com/tutorials/blocks-and-table-view-cells-on-ios--mobile-22982但它仅适用于 UITableViewCell 而不是自定义单元格

                  http://code.tutsplus.com/tutorials/blocks-and-table-view-cells-on-ios--mobile-22982 but it is only for UITableViewCell not the custom cell

                  我能够通过以下方式解决我的问题

                  @interface ProductListVC ()
                  {
                      int count;
                      bool addBtnClicked;
                      bool minusBtnClicked;
                  
                  
                  }
                  
                  @property (strong,nonatomic) NSMutableArray* quantityArray; //stores label
                  @property (strong,nonatomic) NSMutableArray* quantityLabelArray; //stores label
                  
                  - (void)viewDidLoad {
                  
                      [super viewDidLoad];
                  
                      _quantityLabelArray = [[NSMutableArray alloc] init];
                       _quantityArray = [[NSMutableArray alloc] init];
                  
                  //determine num of row prevent index 2 beyond bounds for empty array error i.e insert straight to let's say index 3
                      for (int i=0; i <=[_productArray count]; i++) {
                              [_quantityArray addObject:@"0"];
                  
                          }
                  }
                  
                  
                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                  ................
                  
                  if (!_quantityArray || !_quantityArray.count){
                          cell.lblCount.text = [NSString stringWithFormat:@"%d", 0];
                  
                      }else{
                          cell.lblCount.text = [_quantityArray objectAtIndex:indexPath.row];
                       }
                       [_quantityLabelArray addObject:cell.lblCount];
                  
                  
                      if (addBtnClicked || minusBtnClicked) {
                          //If add or minu button is reloading the cell
                  
                          cell.lblCount.text = [_quantityArray objectAtIndex:indexPath.row];
                          //NSLog(@"%@",cell.lblCount.text);
                          [_quantityLabelArray replaceObjectAtIndex:indexPath.row withObject:cell.lblCount];
                          addBtnClicked = NO;
                          minusBtnClicked = NO;
                  
                      }
                      else
                      {
                  
                      }
                  
                      cell.btnMinus.tag = indexPath.row;
                      cell.btnPlus.tag = indexPath.row;
                      cell.lblCount.tag = indexPath.row;
                  
                      [cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
                      [cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
                  
                      cell.selectionStyle = UITableViewCellSelectionStyleNone;
                      return cell;
                  }
                  
                  
                  
                  #pragma mark - UIButton selector
                  
                  -(void)addItem:(UIButton*)button
                  
                  {
                  
                       NSLog(@"btn tag %ld",(long)[button tag]);
                       NSLog(@"quantitylblarry %@",_quantityLabelArray);
                  
                          UILabel* lblShowCount = [_quantityLabelArray objectAtIndex:[button tag]];
                          if ([lblShowCount.text integerValue]< 10) {
                              count = count+1;
                              lblShowCount.text = [NSString stringWithFormat:@"%ld", [lblShowCount.text integerValue]+1];
                          }
                  
                       NSLog(@"quantity array count %lu",(unsigned long)_quantityArray.count);
                  
                          if (_quantityArray.count > [button tag] ) {
                  
                              [_quantityArray removeObjectAtIndex:[button tag]];
                              [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
                          }
                  
                          else{
                  
                              [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
                          }
                          addBtnClicked = YES;
                  
                          NSLog(@"quantity array %@",_quantityArray);
                          NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
                          NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
                          [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
                  
                      }
                  
                  
                  
                  
                  
                  -(void)deleteItem:(UIButton*)button{
                  
                       NSLog(@"%ld",(long)[button tag]);
                      UILabel* lblShowCount = [_quantityLabelArray objectAtIndex:[button tag]];
                  
                  
                          if ([lblShowCount.text integerValue]>=1) {
                              count = count-1;
                              lblShowCount.text = [NSString stringWithFormat:@"%ld", [lblShowCount.text integerValue]-1];
                  
                          }
                  
                          NSLog(@"%lu",(unsigned long)_quantityArray.count);
                  
                          if (_quantityArray.count > [button tag] ) {
                              [_quantityArray removeObjectAtIndex:[button tag]];
                              [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
                          }
                          else{
                              [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
                  
                          }
                          NSLog(@"%@",_quantityArray);
                          minusBtnClicked =YES;
                  
                          NSLog(@"quantity array %@",_quantityArray);
                          NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
                          NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
                          [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
                  
                   }
                  

                  推荐答案

                  最好使用自定义单元格.检查此 教程 或这个 one,那么你需要将委托添加到这个单元格,所以您可以与 ViewController 同步数量

                  it is better to use custom cell. Check this tutorials or this one, then you will need to add delegate to this cell so that you can sync quantities with you ViewController

                  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                      CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
                      cell.delegate = self;
                      cell.itemIndex = indexPath.row;
                      ....
                      return cell
                  }
                  

                  更新

                  如果您有自定义单元格,您将添加一个 UILabel 插座并在 +/- 按钮的操作中修改其文本,所有这些代码都将在 <代码>CustomTableViewCell.m

                  if you have a custom cell, you would add an UILabel outlet and modify its text in the action of +/- buttons, all this code will be in CustomTableViewCell.m

                  -(void)addItem:(UIButton*)button {    
                      self.itemQuantity++; 
                      [self updateQuantity];
                  }
                  
                  -(void)deleteItem:(UIButton*)button {
                      self.itemQuantity--;
                      [self updateQuantity];
                  }
                  - (void)updateQuantity{
                      self.lblCount.text = [NSStirng stringWithFormat:@"%d", self.itemQuantity];
                      [self.delegate updateItemAtIndex:self.itemIndex withQuantity:self.itemQuantity];
                  }
                  

                  更新:完整的解决方案

                  1.型号

                  @interface Item : NSObject
                  @property (nonatomic, strong) NSString *title;
                  @property (nonatomic) NSInteger quantity;
                  @end
                  
                  @implementation Item
                  
                  @end
                  

                  <强>2.自定义单元格

                  @interface CustomItemTableViewCell : UITableViewCell
                  
                  @property (nonatomic, weak) IBOutlet UILabel *lblTitle;
                  @property (nonatomic, weak) IBOutlet UILabel *lblCount;
                  @property (nonatomic, assign) Item *item;
                  @end
                  
                  @implementation CustomItemTableViewCell
                  
                  - (void)updateCellWithItem:(Item *)item {
                      self.item = item;
                      [self updateCellView];
                  }
                  - (void)updateCellView {
                      self.lblTitle.text = self.item.title;
                      self.lblTitle.text = [NSString stringWithFormat:@"%ld", self.item.quantity];
                  }
                  - (IBAction)addItem:(UIButton*)button {
                      self.item.quantity++;
                      [self updateCellView];
                  }
                  
                  - (IBAction)deleteItem:(UIButton*)button {
                      self.item.quantity--;
                      [self updateCellView];
                  }
                  
                  @end
                  

                  3.TableViewController

                  @interface ItemsTableViewController : UITableViewController
                  @property (nonatomic, strong) NSArray *items;
                  @end
                  
                  @implementation ItemsTableViewController
                  
                  #pragma mark - Table view data source
                  
                  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
                      return 1;
                  }
                  
                  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
                      return self.items.count;
                  }
                  
                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                      CustomItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell" forIndexPath:indexPath];
                  
                      [cell updateCellWithItem:self.items[indexPath.row]];
                  
                      return cell;
                  }
                  

                  这篇关于访问 cellForRowAtIndexPath 之外的自定义单元格属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Retina 显示器 VS 正常显示器色差 下一篇:如何禁用 UIButton 的高亮控制状态?

                  相关文章

                • <tfoot id='Z1HQi'></tfoot>
                  <legend id='Z1HQi'><style id='Z1HQi'><dir id='Z1HQi'><q id='Z1HQi'></q></dir></style></legend>
                  <i id='Z1HQi'><tr id='Z1HQi'><dt id='Z1HQi'><q id='Z1HQi'><span id='Z1HQi'><b id='Z1HQi'><form id='Z1HQi'><ins id='Z1HQi'></ins><ul id='Z1HQi'></ul><sub id='Z1HQi'></sub></form><legend id='Z1HQi'></legend><bdo id='Z1HQi'><pre id='Z1HQi'><center id='Z1HQi'></center></pre></bdo></b><th id='Z1HQi'></th></span></q></dt></tr></i><div id='Z1HQi'><tfoot id='Z1HQi'></tfoot><dl id='Z1HQi'><fieldset id='Z1HQi'></fieldset></dl></div>

                  1. <small id='Z1HQi'></small><noframes id='Z1HQi'>

                        <bdo id='Z1HQi'></bdo><ul id='Z1HQi'></ul>