gpt4 book ai didi

ios - 无法使用 tableview cell.accessoryView 进行 Click 事件

转载 作者:行者123 更新时间:2023-11-28 23:45:59 25 4
gpt4 key购买 nike

这个伟大的社区大家好。我只需要在 tableView 中实现一个简单的删除功能。我知道这可以使用滑动删除( TableView 委托(delegate))来实现。但客户需要这样的东西:

reference_image

我使用 cell.accessoryView = trash_icon; 完成了这个设计。现在的要求是,

  • 当我点击 tableView 单元格时。它必须显示的详细信息车辆。

  • 如果我点击垃圾箱/垃圾桶图标,它必须删除该行。

    我已经使用 cellForRowAtIndexPath 为 Cells 成功导航到车辆详细信息。但坚持执行删除选项。问题是每当我点击垃圾桶图标时,它都会转到车辆详细信息页面。我看过以下内容link1 , link2 , link3等等,但我无法理解实现这一点的想法。我还通过将单元格指定为 cell.accessoryType = UITableViewCellAccessoryCheckmark
    cell.accessoryType = UITableViewCellAccessoryDe​​tailDisclosureButton 尝试了 accessoryButtonTappedForRowWithIndexPath它可以工作,但问题是我无法更改 accessoryType 的默认图像。请多多指教,感谢您的耐心阅读!

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

 - (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

最佳答案

你可以用委托(delegate)设计模式来做

STEP-I : Make the protocol to delegate with cell

这个协议(protocol)你会在UIViewController类中确认并且包含两个方法

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

    protocol ItemCellDelegate :class{
    func cellDidTaaped(withCell cell:ItemCell)
    func accessoryViewDidTapped(withCell cell:ItemCell, state :Bool)
    }

STEP-II : Make the UITableViewCell class to customize the accessory view

  • 使用 UiimageView 初始化 accessoryView
  • 我们将制作两个 UITapGestureRecognizer,当每个人都识别时,代表它的方法将被解雇

注意:我们定义了一个bool Flag,通过协议(protocol)发送给它,判断accessoryView的状态是否需要改变它的图片

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 : Make UIViewControler the delegate of call

tableView调用cellForRowAt时必须确认cell的delegate
功能

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 : Confirm the delegate of cell at UIViewControler class

确认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 方法

关于ios - 无法使用 tableview cell.accessoryView 进行 Click 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com