加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iPhone – 如何在表格单元格上添加可点击按钮?

发布时间:2020-12-14 19:34:49 所属栏目:百科 来源:网络整理
导读:我动态地在表格视图单元格中添加一个按钮.桌子上显示的按钮,但我无法点击它们.这是我的代码, 这是我的tableviewcell类代码: MessageTableViewCell.h #import UIKit/UIKit.h@interface MessageTableViewCell : UITableViewCell {{IBOutlet UIButton *chat_pi
我动态地在表格视图单元格中添加一个按钮.桌子上显示的按钮,但我无法点击它们.这是我的代码,

这是我的tableviewcell类代码:

MessageTableViewCell.h

#import <UIKit/UIKit.h>
@interface MessageTableViewCell : UITableViewCell {
{IBOutlet UIButton *chat_pic_btn;}}
@property (nonatomic,retain) UIButton *chat_pic_btn;
@end;

MessageTableViewCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {chat_pic_btn=[[UIButton alloc]init];
[self.contentView addSubview:chat_pic_btn];}}

MessageTable.m

-(void)customActionPressed :(id)sender
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Button Pressed" 
                                                        message:[NSString stringWithFormat: @"You pressed the custom button on cell"]  
                                                       delegate:self cancelButtonTitle:@"Great" 
                                              otherButtonTitles:nil];
    [alertView show];
}

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

    static NSString *CellIdentifier = @"CellIdentifier";
    MessageTableViewCell *cell = (MessageTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        // cell = [[MessageTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
cell.chat_pic_btn.frame = CGRectMake(180,24,70,35);
            [cell.chat_pic_btn setImage:[UIImage imageNamed:@"done.png"]forState:UIControlStateNormal];
            [cell.chat_pic_btn addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown];
return cell;
}

请帮帮我.
谢谢.

解决方法

我建议你删除TableViewCell中的Button插座.并在cellForRowAtIndexPath中动态创建您的按钮:
我创建了一个SampleCell类,UITableViewCell的子类,它有一个名为“lbl”的UILabel插座.
这应该适用于您的代码,假设您的选择器与您放置tablview的同一个类;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SampleCell";
    SampleCell *cell = (SampleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SampleCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (SampleCell *)currentObject;
                break;
            }
        }
    }
    // Configure the cell.
    cell.lbl.text = @"Hello";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //set the position of the button
    button.frame = CGRectMake(cell.frame.origin.x + 100,cell.frame.origin.y + 20,100,30);
    [button setTitle:@"World" forState:UIControlStateNormal];   
    [button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor= [UIColor clearColor];
    [cell.contentView addSubview:button];

    return cell;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读