简体   繁体   English

单元格中的可单击的imageview和buttonview(UITableView或collectionView)

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

I have a UI design problem about ios objective-c. 我有关于ios objective-c的UI设计问题。 there are thousands of product images need to be displayed in a single page,5 products every line,and 2 lines information for each product under each image. 需要在一个页面中显示数千个产品图像,每行显示5个产品,每个图像下每个产品有2个行信息。 If user clicks the product image, I need to add it to shopping cart. 如果用户点击产品图片,我需要将其添加到购物车。 moreover,I need to add a small button under each product image.If user click the button, I will display a window to allow user to write notes for that product. 此外,我需要在每个产品图像下添加一个小按钮。如果用户单击该按钮,我将显示一个窗口,允许用户为该产品编写注释。

How to design this kind of UI? 如何设计这种UI? thank you. 谢谢。

In the cellForRowAtIndexPath: ip is indexPath 在cellForRowAtIndexPath中:ip是indexPath

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

Go to view controller and add this function which will be called every time any button will be called... Each button has unique tag (because of the raw numbers) and so you can make condition to each one by its number... 转到查看控制器并添加此功能,每次调用任何按钮时都会调用该功能...每个按钮都有唯一的标签(因为原始数字),因此您可以通过其编号为每个按钮创建条件...

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

I wrote it from my iPhone so I will write more when I get to a PC... 我是从iPhone上写的,所以当我上电脑时我会写更多...

Here is the code : 这是代码:

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];
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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM