Ios UICollectionView中每个UIButton的特定操作

Ios UICollectionView中每个UIButton的特定操作,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我有4个UICollectionCell(名为CustomCell),每个都包含一个按钮。每个按钮必须具有不同的行为。我通过使用NSArray(谢谢stackOverflow!)和设置不同的字体和选择器来实现这一点,但我觉得一定有一种更简单的方法,我没有找到 这是我的密码: #import "MainViewController.h" #import "customCell.h" @interface MainViewController () @end @implementation M

我有4个UICollectionCell(名为CustomCell),每个都包含一个按钮。每个按钮必须具有不同的行为。我通过使用NSArray(谢谢stackOverflow!)和设置不同的字体和选择器来实现这一点,但我觉得一定有一种更简单的方法,我没有找到

这是我的密码:

#import "MainViewController.h"
#import "customCell.h"

@interface MainViewController ()

@end

@implementation MainViewController
{
    NSArray * arrayOfImg;
    NSArray * arrayOfMethods;

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[self myCollectionView]setDataSource:self];
    [[self myCollectionView]setDelegate:self];

    arrayOfImg = @[NSArray arrayWithObjects:@"note_button", @"photo_button", @"sync_agenda_button", @"sync_contacts_button", nil];

    arrayOfMethods = @[@"showText", @"showText2", @"showText3", @"showText4"];

}

-(void)showText
{
    NSLog(@"text1!");
}

-(void)showText2
{
    NSLog(@"text2");
}

-(void)showText3
{
    NSLog(@"text3");
}

-(void)showText4
{
    NSLog(@"text4");
}

#pragma mark CollectionView methods

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}
这里是我设置不同行为的地方:

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    [cell.myButton setBackgroundImage:[UIImage imageNamed:[arrayOfImg objectAtIndex:indexPath.item]] forState:UIControlStateNormal];

    SEL selector = NSSelectorFromString([arrayOfMethods objectAtIndex:indexPath.item]);
    [cell.myButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

@end

那么,有什么问题?这不是一个复制品吗?我只是觉得它没有android那么直观,所以我想也许我错过了一个部分。此外,我没有发现如何在网络上进行这种常见的操作,这让我很惊讶,你所拥有的一切都很好。或者,您可以为按钮指定相同的目标/选择器,然后按下后,确定发送方和分支的索引路径。选择器@选择器(按钮位于此处)