Custom Cell中的多个按钮。单击哪一行按钮?

时间:2012-02-03 18:07:49

标签: iphone uitableview nsindexpath custom-cell

我在学生项目上工作,我需要知道点击哪一行的按钮。

每一行都有不同的标签,一个像按钮,一个不喜欢按钮。

我知道如何使用NSIndexPath.row来检测按下哪一行,但在这种情况下,我需要知道哪一行以及该行中的哪个按钮被按下。

我已经在SO上搜索了一个很好的解决方案,可以检测每行多个按钮时单击按钮的行。

根据我的理解阅读其他帖子,使用button.tag可能非常难以预测,所以我宁愿使用不同的方法。

我见过许多已实现此功能的应用,因此必须有一种最佳方式来实现此功能。有没有人有任何好的建议?

代码:

SearchCell.h

@interface SearchCell : UITableViewCell {

IBOutlet UIButton *likebutton2;
IBOutlet UIButton *dislikebutton2;

}

@property (nonatomic,retain) IBOutlet UILabel *track_label;
@property (nonatomic,retain) IBOutlet UILabel *artist_label;
@property (nonatomic,retain) IBOutlet UILabel *album_label;

@property (nonatomic,retain) IBOutlet UIButton *likebutton2;
@property (nonatomic,retain) IBOutlet UIButton *dislikebutton2;

@property (nonatomic, copy) void (^onButton)(UIButton *button);

- (void)buttonAction:(UIButton *)sender;

@end

SearchCell.m

#import "SearchCell.h"
#import "RootViewController.h"

@implementation SearchCell

@synthesize likebutton2, dislikebutton2, track_label, artist_label, album_label;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code.

}
    return self;
}

- (void)buttonAction:(UIButton *)sender
{
    self.onButton(sender);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

// Configure the view for the selected state.
}

- (void)dealloc {
    [super dealloc];
}

@end

的cellForRowAtIndexPath

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

NSString *uniqueIdentifier = @"searchCell";

SearchCell *cell = nil;

Search *currentSearch = nil;

cell = (SearchCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];

if (tableView == [[self searchDisplayController] searchResultsTableView]) //Just from some previous debugging
{
    currentSearch = [[searchxmlParser searchhits] objectAtIndex:indexPath.row];
}

if(!cell)
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects) {
        if ([currentObject isKindOfClass:[SearchCell class]]) {
            cell = (SearchCell *)currentObject;

            [cell.likebutton2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [cell.dislikebutton2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            cell.likebutton2.tag = 1;
            cell.dislikebutton2.tag = 2;    

            break;
        }
    }

}

cell.onButton = ^(UIButton *theButton) {
    [self handleButton:theButton indexPath:indexPath];
}

cell.track_label.text = [currentSearch track];
cell.artist_label.text = [currentSearch artist];

return cell;    
}

感谢您的帮助:)!

3 个答案:

答案 0 :(得分:3)

为什么不做以下事情:

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

    NSString *reuseIdentifier = @"MyCell";
    MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (cell == nil) {

        cell = [[[MyCustomCell alloc] init] autorelease];

        [cell.firstButton addTarget:self action:@selector(firstButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.secondButton addTarget:self action:@selector(secondButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }

    [cell.firstButton setTag:indexPath.row];
    [cell.secondButton setTag:indexPath.row];

    // other cell setup...

    return cell;
}

- (void)firstButtonPressed:(id)sender {

    NSInteger cellRow = [sender tag];

    // do things...
}

- (void)secondButtonPressed:(id)sender {

    NSInteger cellRow = [sender tag];

    // do things...
}

这假设您UITableView中只有一个部分,使用多个部分执行此操作会有点复杂。

答案 1 :(得分:1)

使用.tag属性,它非常可靠且可预测。

如果您不这么认为,请显示一些代码。

答案 2 :(得分:1)

我认为使用块是一种很好的方法,仍然使用@ EllNeal解决方案中的标签:

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

NSString *reuseIdentifier = @"MyCell";
MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if (cell == nil) {

    cell = [[[MyCustomCell alloc] init] autorelease];

    [cell.button1 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.button2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button1.tag = 1;
    cell.button2.tag = 2;
}

cell.onButton = ^(UIButton *theButton) {
     [self handleButton:theButton indexPath:indexPath];
}

return cell;

然后你告诉你按哪个按钮的处理程序就像这样:

- (void)handleButton:(UIButton *)button indexPath:(NSIndexPath *)indexPath
{
   //Use tag of button to identify which button was tapped, as well as the indexPath
}

您的手机代码看起来有点像这样:

@interface MyCustomCell
...
- (void)buttonAction:(UIButton *)sender
@property (nonatomic, copy) void (^onButton)(UIButton *button);
...
@end

@implementation
...
- (void)buttonAction:(UIButton *)sender
{
    self.onButton(sender);
}
...
@end

希望这有帮助!