无法将Tableview cell.accessoryView用于Click事件

时间:2018-10-05 11:28:06

标签: ios uitableview

为这个伟大的社区打招呼。我只需要在tableView中实现一个简单的删除功能。我知道可以使用滑动删除(表视图委托)来实现。但是客户需要这样的东西:

reference_image

我已经使用cell.accessoryView = trash_icon;完成了此设计。现在的要求是

  • 当我点击tableView单元格时。它必须显示 车辆。
  • 如果我点击垃圾桶/垃圾桶图标,则必须删除该行。

    我已成功使用cellForRowAtIndexPath导航到单元格中的车辆详细信息。但停留在实现删除选项。问题是,每当我点击垃圾桶图标时,它就会转到车辆详细信息页面。我已经研究了以下link1link2link3等内容,但是我不知道实现该想法的想法。另外,我通过将单元格指定为accessoryButtonTappedForRowWithIndexPath来尝试cell.accessoryType = UITableViewCellAccessoryCheckmark
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton 它可以工作,但问题是我无法更改附件类型的默认图像。请指导我,感谢您耐心阅读!

编辑:到目前为止,我尝试过的操作是:

 - (UITableViewCell *)tableView:(UITableView *)tableView 

cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//    cell.backgroundColor = UIColorFromRGB(0x717579);
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundView.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:NSLocalizedString(@"font_name",nil) size:13];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.textLabel.textColor = [UIColor whiteColor];
UIImageView *trash_icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_vehicle_delete.png"]];
if(indexPath.section != [vehicleDetails count]) {
    //cell.accessoryView = trash_icon;  --> If I keep this only Image appears but the action doesn't gets triggered
} else {
     cell.accessoryType = UITableViewCellStyleDefault
}

if(indexPath.section == [vehicleDetails count])
{
    addnewLabel.font = [UIFont fontWithName:NSLocalizedString(@"font_name",nil) size:13];
    addnewLabel.backgroundColor = [UIColor clearColor];
    addnewLabel.textColor = [UIColor whiteColor];
    addnewLabel.text = NSLocalizedString(@"vehicle_new",nil);
    [cell addSubview:addnewLabel];
    cell.textLabel.text = @"";
    UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"input-plus.png"]]autorelease];
    background.frame = CGRectMake(0, 0, 300, 35);
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        background.frame = CGRectMake(10, 0, 300, 35);

    cell.backgroundView = [[[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 300, 35)]autorelease];
    [cell.backgroundView addSubview:background];
}else
{
    cell.textLabel.text =[NSString stringWithFormat:@"Vehicle #%li: %@",indexPath.section+1,[[vehicleDetails objectAtIndex:indexPath.section] valueForKey:@"make"]];
    UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"text field 2x.png"]]autorelease];
    background.frame = CGRectMake(0, 0, 300, 35);
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        background.frame = CGRectMake(10, 0, 300, 35);


    cell.backgroundView = [[[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 300, 35)]autorelease];
    [cell.backgroundView addSubview:background];
}

return cell;
}

以上方法成功调用了

 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
//here I do the delete stuff

但是输出看起来像:

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以使用委托设计模式

  

STEP-I:使协议与单元格委派

此协议将在UIViewController类中得到确认,其中包含两种方法

  • cellDidTaaped :当单元格点击时调用的方法
  • accessoryViewDidTapped :当AccessoriesView确实点击

    时调用的方法
    protocol ItemCellDelegate :class{
        func cellDidTaaped(withCell cell:ItemCell)
        func accessoryViewDidTapped(withCell cell:ItemCell, state :Bool)
    }
    
  

STEP-II:制作UITableViewCell类以自定义附件视图

  • 使用UiimageView初始化附件视图
  • 我们将在每个人都识别出两个UITapGestureRecognizer时,委托 会被解雇

注意:我们定义了一个bool标志,通过协议将其发送,以确定是否需要更改attachView的图片

class ItemCell: UITableViewCell {

weak var  delegte:ItemCellDelegate?

private var accessoryViewTappedFlag = false


override func awakeFromNib() {
    super.awakeFromNib()

    self.selectionStyle = UITableViewCellSelectionStyle.none
    let cellGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ItemCell.cellViewDidTapped))
    self.addGestureRecognizer(cellGestureRecognizer)
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    imageView.isUserInteractionEnabled = true
    self.accessoryView = imageView
    (self.accessoryView as! UIImageView).image = #imageLiteral(resourceName: "Birthdays")

    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ItemCell.accessoryViewDidTapped))
    accessoryView!.addGestureRecognizer(gestureRecognizer)
}

func accessoryViewDidTapped() {
    self.accessoryViewTappedFlag = !accessoryViewTappedFlag
    delegte?.accessoryViewDidTapped(withCell: self, state: accessoryViewTappedFlag)
}
func cellViewDidTapped() {
    delegte?.cellDidTaaped(withCell: self)
}

}
  

STEP-III:让UIViewControler成为调用的委托

当tableView调用cellForRowAt
时,我们必须确认单元格的委托 功能

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
    cell.delegte =  self
    ....
    return cell
}
  

STEP-IV:确认UIViewControler类的单元格委托

在listItemsVC(UIViewControler)上确认ItemCellDelegate并实现方法

extension listItemsVC:ItemCellDelegate{

    func cellDidTaaped(withCell cell: ItemCell) {
        let indexPath = tableView.indexPath(for: cell)
    //.....
    }
    func accessoryViewDidTaaped(withCell cell: ItemCell, state: Bool) {
        let indexPath = tableView.indexPath(for: cell)
        if let accessoryView  = cell.accessoryView as? UIImageView {
            if state{
                accessoryView.image = #imageLiteral(resourceName: "Trips")
            }else{
                accessoryView.image = #imageLiteral(resourceName: "Folder")
            }
        }
    }
}

注意:现在不需要didSelectRowAt方法

答案 1 :(得分:0)

因此,最后我通过在附件视图中添加自定义按钮并为此添加了选择器(通过处理删除功能)来获得解决方案

 - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        ...
        ...

        if(indexPath.section != [vehicleDetails count]) {
        //Adding the custom button to the accessory view
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(275, 140, 30, 30);
            [button setImage: trash_icon.image forState:UIControlStateNormal];

            //Adding the selector for the Button
            [button addTarget:self action:@selector(vehicleDeleteHandler:) forControlEvents:UIControlEventTouchUpInside];

            button.tag = indexPath.section;
            cell.accessoryView  = button;
        }
        ...
        ...
}

//Defining the selector function for the button.
-(void)vehicleDeleteHandler: (id)sender {
//    if([sender tag] == 0) {
    NSLog(@"%ld",(long)[sender tag]);
    //Delete Functionality
    ...
    ...
}