2013-08-02 63 views
1

我试图去点击编辑按钮时,苹果的闹钟,自定义视图覆盖自定义UITableViewCell在编辑模式下添加xib作为掩盖自定义UITableViewCell的视图

上面的代码:

// CGRect frame = [tableView rectForRowAtIndexPath:indexPath]; 
    // CGPoint yOffset = self.tableViewBlock.contentOffset; 
    // CGRect newFrame = CGRectMake(frame.origin.x, (frame.origin.y - yOffset.y + 45), frame.size.width, frame.size.height); 
    CallBlock_Date_EditMode *viewController = [[CallBlock_Date_EditMode alloc] initWithNibName:@"CallBlock_Date_EditMode" bundle:nil]; 

    // self.view.frame = newFrame; 
    // [self.view addSubview:viewController.view]; 
    // [self addChildViewController:viewController]; 

    UITableViewCell *cell = (UITableViewCell*)[self.tableViewBlock cellForRowAtIndexPath:indexPath]; 
    [cell.contentView addSubview:viewController.view]; 

盖上特定的细胞,当我把下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
  • 只是为了确保大小是确定的(尽管当我点击一个按钮即xib应用程序崩溃,甚至没有一个单一的错误)。

但我想像苹果的闹钟(实际上,模仿它),点击我的编辑按钮,我的自定义UITableViewCell将得到与此xib作为视图的封面。

也许有更好的方法来做到这一点?

编辑:

我更新的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    CallBlock_TableCell *cell = (CallBlock_TableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

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

    // Configure the cell... 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)configureCell:(CallBlock_TableCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.accessoryType = self.isEditing ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; 

    CallBlock_ByDate *callBlock = (CallBlock_ByDate*)[fetchedObjects objectAtIndex:indexPath.row]; 
    cell.labelTime.text = callBlock.startDate; 
    cell.labelRepeat.text = callBlock.repeat; 
    cell.labelTextLabel.text = callBlock.label; 
    cell.switchCallBlock.on = YES; 
    cell.switchCallBlock.tag = (NSInteger)indexPath.row +1; 
    [cell.switchCallBlock addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged]; 
    cell.switchCallBlock.hidden = self.isEditing ? YES : NO; 

    if (self.isEditing) 
    { 
     cell.switchCallBlock.hidden = YES; 
     UIButton *btnArrow = [[UIButton alloc] init]; 
     btnArrow.frame = CGRectMake(282.0, 31.0, 18.0, 21.0); 
     [btnArrow setBackgroundImage:[UIImage imageNamed:@"arrow_FWR_off"] forState:UIControlStateNormal]; 
     [btnArrow setBackgroundImage:[UIImage imageNamed:@"arrow_FWR_on"] forState:UIControlStateHighlighted]; 
     btnArrow = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [btnArrow addTarget:self action:@selector(handleTapToEdit:) forControlEvents:UIControlEventTouchUpInside]; 
     btnArrow.tag = indexPath.row + 1; 
     [cell.contentView addSubview:btnArrow]; 
     [cell.contentView bringSubviewToFront:btnArrow]; 
    } 
} 

但我不能让btnArrow出现在UTableView

回答

1

您发生崩溃的原因是因为没有任何内容保留您的CallBlock_Date_EditMode视图控制器。您将其视图作为子视图添加到您的单元格中,但没有任何内容维护对视图控制器的引用,因此将其解除分配,然后当按下应该将消息传递给视图控制器的按钮时,它将被发送到解除分配对象,你会崩溃。

有两种可能的解决方案。首先,您可以将视图控制器存储在您的某个属性中以维护对其的引用,以便它不会释放。这在很大程度上可能不是你想要的。

相反,我会建议做的是不要让你的CallBlock_Date_EditMode UIViewController,而是使它成为一个UIView。您可能想知道“但是我怎样才能在没有UIViewController的情况下使用xib?”。我会做类似如下:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"CallBlock_Date_EditMode" owner:self options:nil] objectAtIndex:0]; 
     self.myEditButton = (UIButton *)[view viewWithTag:2]; 

     [self addSubview:view]; 
    } 
    return self; 
} 

这将是您自定义的UIView里面的代码,将加载在厦门国际银行文件,并将其添加为子视图。为了访问你的子视图,你必须在界面构建器中使用标签,所以你失去了绘制/连接IBOutlets的便利......但最终,它比分配/存储一堆不必要的UIViewControllers好得多。

+0

它的工作原理,但我最终认为它太复杂了,我正在做一个不同的方法。 –

1

如果我理解你是正确的,并且你想模仿苹果预装的闹钟功能,那么你的解决方案比创建自定义视图要简单得多。看起来他们所做的只是将开关切换到隐藏状态,并向单元添加一个泄露指示器。这是我会做...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     bool hide = (tableView.editingStyle == UITableViewCellEditingStyleDelete); // set to true or false depending on if the table is in editing mode 

     for (UIView *sv in [cell subviews]) { 
      if([sv isKindOfClass:[UISwitch class]]) { // find the on-off switch 
       [sv setHidden:hide]; // hide the switch depending on the t/f value of hide 
       break; 
      } 
     } 
     if(hide) { // adds the arrow like in apple's alarm clock table if the cell is in edit mode 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     else { 
      [cell setAccessoryType:UITableViewCellAccessoryNone]; 
     } 

return cell; 
} 
+0

这是一个自定义的UITableViewCell,并且不显示该指示符。 –

相关问题