<small id='w4Pyi'></small><noframes id='w4Pyi'>

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

        iOS UITableViewCells 行被回收并且标签不起作用

        时间:2023-09-10

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

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

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

                  本文介绍了iOS UITableViewCells 行被回收并且标签不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在为单元格中的按钮设置标签方面寻求一些帮助.这是一个链接到我之前发布的问题:iOS 使用 NSDictionary 将数据加载到节和行中

                  I would like to seek some help in setting tag for buttons in cells. This is a question with a link to the previous I posted : iOS Using NSDictionary to load data into section and rows

                  但是,虽然我现在可以动态传递数据,但我在每一行上的续订按钮似乎无法获取数据,并且只会在选择该部分中的任何行时检测到每个部分的相同书名.

                  However, though I could pass in data dynamically now, my renewal button on each of the rows could not seem to get the data and would only detect the same book title of each section when any of the rows in the section is selected.

                  根据我目前所阅读的内容,这是因为按钮正在被回收,因此无法检测到正确选择了哪本书.我试过设置标签:

                  Based on what I've read so far, it's because the button is being recycled hence, unable to detect which book is being selected properly. I've tried to set tag:

                  cell.renewButton.tag = indexPath.row;
                  

                  我的代码现在的样子:

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                  
                  static NSString *CellIdentifier = @"Cell";
                  UserCustomCell *cell = (UserCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                  cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
                  
                  if (cell == nil) {
                      [[NSBundle mainBundle] loadNibNamed:@"UserCustomCell" owner:self options:nil];
                      cell = userCustomCell;
                      self.userCustomCell = nil;
                      cell.renewButton.tag = indexPath.row;
                  }
                  
                  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                      cell.bookTitle.frame = CGRectMake(12, 0, 550, 40);
                      cell.renewButton.frame = CGRectMake(600, 14, 68, 24);
                  }
                  [cell.renewButton useBlackActionSheetStyle];
                  

                  //########## 编辑从这里开始dataSource = [[NSMutableDictionary alloc] init];//这需要是一个 ivar

                  //########## EDIT STARTS HERE dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar

                  for (NSDictionary *rawItem in myArray) {
                      NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key
                  
                      NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date
                  
                      if (!section) { // If there is no section then create one and add it to the dataSource
                          section = [[NSMutableArray alloc] init];
                          [dataSource setObject:section forKey:date];
                      }
                  
                      [section addObject:rawItem]; // add your object
                  }
                  self.dataSource = dataSource;
                  //NSLog(@"Data Source Dictionary: %@", dataSource); 
                  
                  NSArray *sections =[[dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
                  NSString *sectionTitle = [sections objectAtIndex:indexPath.section];
                  
                  NSArray *items = [dataSource objectForKey:sectionTitle];
                  
                  NSDictionary *dict = [items objectAtIndex:indexPath.row];
                  cell.bookTitle.text = [dict objectForKey:@"name"];
                  cell.detail.text = [NSString stringWithFormat:@"Due Date: %@ Due Time: %@", 
                                      [dict objectForKey:@"date"], [dict objectForKey:@"time"]];
                  
                  
                  cell.renewButton.tag = indexPath.row;
                  return cell;   
                  

                  }

                  但它根本不起作用.非常感谢任何建议:) 谢谢!!

                  but it doesn't work at all. Would be sooooo grateful for any suggestions :) Thank you!!

                  P.S:我的 xcode 副本没有更新,只更新到 version4.看到有人提到在 DataModel 中存储标签状态,但它仅在较新版本中可用.:)

                  P.S: My copy of xcode is not updated, only till version4. Saw some people mentioning storing of tag status in DataModel but it's only available in newer versions. :)

                  推荐答案

                  我看不到你的 cell.renewButton 被分配了一个选择器方法(应该在点击按钮时触发的方法).

                  I could not see your cell.renewButton being assigned a selector method (the method that should be triggered on tapping the button).

                  [cell.renewButton addTarget:self action:@selector(renewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
                  

                  另外,我会指定一个带有偏移量的标记号,因为标记为 0 几乎就像根本没有标记一样.tableView 的第一行将给出 indexPath.row = 0.

                  Also, I would specify a tag number with an offset, as tag of 0 is almost like not tagging at all. First row of tableView will give indexPath.row = 0.

                  在您的代码之上,

                  #define OFFSET 100 /* Or any number greater than 0 */
                  

                  在 cellForRowAtIndexPath 中,

                  In cellForRowAtIndexPath,

                  ...
                  [cell.renewButton addTarget:self action:@selector(renewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
                  cell.renewbutton.tag = indexPath.row + OFFSET;
                  ...
                  

                  在renewButtonPressed方法中,

                  In the renewButtonPressed method,

                  -(void)renewButtonPressed:(id)sender
                  {
                      tappedNum = [sender tag] - OFFSET;
                      /* do your stuff */
                  }
                  

                  tappedNum 将为您提供点击按钮的行,从 0 开始.

                  tappedNum will give you the row that the button is tapped, starting with 0.

                  这篇关于iOS UITableViewCells 行被回收并且标签不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有办法从代码创建 ACTION_NDEF_DISCOVERED 意图 下一篇:CCLOG 不显示

                  相关文章

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

                • <tfoot id='MTL39'></tfoot>
                    <legend id='MTL39'><style id='MTL39'><dir id='MTL39'><q id='MTL39'></q></dir></style></legend>
                    • <bdo id='MTL39'></bdo><ul id='MTL39'></ul>

                      <small id='MTL39'></small><noframes id='MTL39'>