单元格中的可点击图像视图和按钮视图(UITableView 或 collectionView)

clickable imageview and buttonview in a cell(UITableView or collectionView)

我有一个关于 ios objective-c 的 UI 设计问题。一个页面需要显示几千张产品图片,每行5个产品,每张图片下2行信息。如果用户点击产品图片,我需要将其添加到购物车。此外,我需要在每个产品下添加一个小按钮 image.If 用户单击该按钮,我将显示一个 window 以允许用户为该产品写注释。

这种UI如何设计?谢谢。

在 cellForRowAtIndexPath 中: ip 是 indexPath

cell.btn.tag = ip.row;
[cell.btn addTarget:self action:@selector(btnclk:) forControlEvents:UIControlEventTouchUpInside];

转到视图控制器并添加此函数,每次调用任何按钮时都会调用该函数...每个按钮都有唯一的标签(因为原始数字),因此您可以通过其为每个按钮设置条件数...

-(void)btnclk:(UIButto*)sender
{
     if (sender.tag == 0) 
      // btn clicked from cell number 0
}

我是用 iPhone 写的,所以当我拿到 PC 时我会写更多...

cell.yourImageView.tag = ip.row;
cell.youreImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
// this maybe repeat to addGesture
// you can user custom UITableViewCell to avoid it
[cell.youreImageView addGestureRecognizer:tap];


- (void)tapClick:(id)sender
{
    UITapGestureRecognizer *tap = sender;
    UIView *view = tap.view;
    if (view.tag == 0) {
        // row 1
    }
}

这是代码:

UIButton*btnLink;//create button

btnLink=[[UIButton alloc]initWithFrame:CGRectMake(8, 8, self.view.frame.size.width, 77)];//set frame

[btnLink setTitle:[@"yourTitle" forState:UIControlStateNormal];

btnLink.tag = indexPath.row;//set tag 

[btnLink addTarget:self action:@selector(linkClick:) forControlEvents:UIControlEventTouchUpInside];//set action

[cell addSubview:btnLink];



-(IBAction)linkClick:(id)sender{

UIButton *btnSender = sender;

NSLog(@"btnSender.tag====>%ld",(long)btnSender.tag);//check your clicked button tag number

NSDictionary *dS;

dS= [yourArray objectAtIndex:btnSender.tag];

NSString*myurl=[dS objectForKey:@"link"];

NSURL *url = [NSURL URLWithString:@"yourURL OR ANYTHING"];
[[UIApplication sharedApplication] openURL:url];