0

我正在做一个学生项目,我需要知道点击了哪一行中的哪个按钮。

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

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

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

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

我见过很多应用程序都实现了这个,所以必须有一个最佳的方法来做到这一点。有人有什么好的建议吗?

代码:

搜索单元.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

搜索细胞.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;    
}

谢谢你的帮忙 :)!

4

3 回答 3

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使用多个部分执行此操作有点复杂。

于 2012-02-03T18:23:25.267 回答
1

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

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

于 2012-02-03T18:14:20.943 回答
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

希望这可以帮助!

于 2012-02-03T19:49:08.423 回答