【问题标题】:custom UITableViewCell know what cell was a check box modified自定义 UITableViewCell 知道哪个单元格被复选框修改
【发布时间】:2013-05-13 10:42:36
【问题描述】:

我有一个带有复选框的自定义单元格,

一切正常,复选框根据我传递给子类 UITableViewCell 的字典进行检查,

但现在我需要将我的复选框已修改的确切单元格传递给具有表格视图的类,以便我可以将可变字典设置为该特定单元格的新选中或未选中状态,

那么该怎么做呢?我应该使用委托吗?这很好,但问题是,我怎么知道我的复选框在哪个单元格被修改了?

【问题讨论】:

  • 这应该会有所帮助。 stackoverflow.com/questions/1750753/…
  • 嗨@MarkM,我想到了这个,但是我的didSelectRow,没有被调用,我在didSelectRow上显示另一个视图控制器,我什至做了一个日志,但是在检查单元格时不会调用它,
  • 是的,didSelect 只有在没有其他东西消耗单元格的触摸时才会被调用。如果单元格包含按钮、文本字段、开关、滑块等......那么该控件将消耗触摸(如果触摸在其中)并停止 didSelect 触发。

标签: ios uitableview


【解决方案1】:

您可以使用这样的委托...

MyCell.h

@protocol MyCellDelegate <NSObject>

-(void)cellCheckBoxWasChanged:(MyCell *)cell;

@end

@interface MyCell : NSObject

@property (nonatomic, weak) id <MyCellDelegate> delegate;

@end

MyCell.m

@implementation MyCell

- (void)checkBoxChanged
{
    [self.delegate cellCheckBoxWasChanged:self];
}

@end

然后找到你可以做的索引...

TableViewController.m

- (void)cellCheckBoxWasChanged:(MyCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // do something to your array.
}

【讨论】:

    【解决方案2】:

    为什么不在委托方法中也将 UITableViewCell 传递为self

    因此,使用该单元格,您可以通过

    获得索引路径
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    

    【讨论】:

      【解决方案3】:

      根据cellForRowAtIndexPath:方法中的单元格Indexpath设置每个Checkboxes的标签值。

         UIButton *checkboxes = customCell.checkButton
         [checkboxes setTag:indexPath.row];
      

      然后在按钮操作方法中。

      检查 senders.Tag 值以获取按下按钮的确切行

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
      

      【讨论】:

      • cellForRowAtIndexPath 需要 NSIndexPath 而不是 int!
      • 如果您有超过 1 个部分怎么办?
      • 有很多解决方法可以实现这一点,您可以为不同部分的按钮设置不同的 IBAction,另一种方法是,继续增加标签号。并在 ibaction 中检查 if(tag >X && tag
      • 我认为有更好的解决方案涉及使用 OO 技术。
      • 肯定有,但我只是发布了最适合我的内容。
      【解决方案4】:

      你可以这样做 -

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      static NSString *CellIdentifier = @"Cell";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
      
          if (cell == nil) 
          {
              cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
          }
      
       if([[[Usercontacts objectAtIndex:indexPath.row]objectForKey:@"isChecked"]isEqualToString:@"NO"])
              {
                  [checkUncheckBtn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal];
                   checkUncheckBtn.tag=1; 
              }
              else
              {
                  [checkUncheckBtn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal];
                    checkUncheckBtn.tag=2; 
              }
             [checkUncheckBtn addTarget:self action:@selector(checkUncheckBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
      
          return cell;
      }
      

      当你执行checkUncheckBtnPressed: 方法时,它看起来像

      -(void)checkUncheckBtnPressed:(id)sender
      {
      
          UIButton *btn=(UIButton*)sender;
          UITableViewCell *cell =(UITableViewCell *) [sender superview] ;
          NSIndexPath *_indxpath = [createGroupContactsTableView indexPathForCell:cell];
      
          if(btn.tag==1)
          {
              [btn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal];   
              btn.tag=2;
              [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"YES" forKey:@"isChecked"];
          }
          else 
          {
      
              [btn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal];   
               btn.tag=1;
              [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"NO" forKey:@"isChecked"];
      
          }
      
      
      }
      

      【讨论】:

      • UITableViewCell *cell =(UITableViewCell *) [sender superview] ; 不可靠。复选框更有可能是 tablesView 的 contentView 的子视图,甚至可能嵌套更深。
      • 是的,这是可能的,但根据我的项目,我根据我的需要创建了。如果你希望你可以嵌套更多
      【解决方案5】:

      这是让 Cells 监听来自复选框的事件并使用委托模式将它们转发到 UITableViewController 的替代方法:

      让 UITableViewController 监听来自复选框的事件并使用以下代码来确定单元格的 NSIndexPath:

      @implementation UITableView (MyCategory)
      
      -(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
          if([component isDescendantOfView:self] && component != self) {
              CGPoint point = [component.superview convertPoint:component.center toView:self];
              return [self indexPathForRowAtPoint:point];
          }
          else {
              return nil;
          }
      }
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-16
        • 2018-03-12
        • 2015-08-07
        • 1970-01-01
        • 2016-06-13
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多