0

どの行のどのボタンがクリックされたかを知る必要がある学生プロジェクトに取り組んでいます。

各行には、好きなボタンと嫌いなボタンの 1 つずつ、異なるラベルがあります。

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

助けてくれてありがとう:)!

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...
}

これは、 にセクションが 1 つしかないことを前提としてい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 に答える