3

私はiphoneを初めて使用します。少し疑問があります。つまり、テーブルビューを作成して、すべての本の名前とその特定の本へのダウンロードオプションを次のように各セルに配置しています。

Genesis    Download
Exodus     Download
Leviticus  Download

上記の創世記、出エジプト記、レビ記は本の名前であり、ダウンロードはこのような本をダウンロードするためのボタンです。テーブルビューに66の異なる本があります。ここで私の質問は、ダウンロードボタンをクリックすると、それに対応する本の名前を取得したいです。テーブルビューセル。私のコードは以下のようなものです

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 66;

}



- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UIButton *downloadButton = nil;
//this is the custom cell i have created one class for this in that i am place the string titlelabel.
        CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) 
        {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
             downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
            downloadButton.frame = CGRectMake(220,10,50,30);
            [downloadButton setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
            [downloadButton addTarget:self action:@selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            downloadButton.backgroundColor = [UIColor clearColor];
            downloadButton.userInteractionEnabled = YES;
            downloadButton.highlighted = YES;
            [cell.contentView addSubview:downloadButton];
      }    
        NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
        cell.TitleLabel.text = titleLabel;


        return cell;
    }

    -(void)downloadButtonClicked:(id)sender{

    }
4

3 に答える 3

3

ボタンアクションでは、superViewにアクセスしてセルを取得できます

-(void)downloadButtonClicked:(id)sender{
    UIButton *button = (UIButton*)sender;
    UIView *view = button.superview; //Cell contentView
    UITableViewCell *cell = (UITableViewCell *)view.superview;
    cell.textLabel.text; //Cell Text
}
于 2012-06-26T07:35:30.747 に答える
1
first set the tag of your label say it is 100.

//in the button click method...

UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

UILabel *lbl = (UILabel *)[cell viewWithTag:100];//this is for custom label 

NSLog(@"label text =%@",lbl.text);

else  NSLog(@"label text =%@",cell.titleLabel.text);
于 2012-06-26T07:36:01.670 に答える
0

テーブルビューセルをタップすると、Delegate関数が呼び出され、インデックスパスセクションと行の値が表示されます。テーブルセルにボタンを配置するときは、タグ--indexpath.rowをボタンタグに割り当てるだけです。ボタン関数を呼び出すときにこのタグを見つけ、配列上のそのインデックスの値を取得するためにarayインデックスを見つけます。

于 2012-06-26T07:39:51.557 に答える