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

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

概述我动态地在表格视图单元格添加一个按钮.桌子上显示的按钮,但我无法点击它们.这是我的代码, 这是我的tableviewcell类代码: MessageTableViewCell.h #import <UIKit/UIKit.h>@interface MessageTableViewCell : UITableViewCell {{IBOutlet UIButton *chat_pic_btn; 我动态地在表格视图单元格中添加一个按钮.桌子上显示的按钮,但我无法点击它们.这是我的代码,

这是我的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;}
总结

以上是内存溢出为你收集整理的iPhone – 如何在表格单元格上添加可点击按钮?全部内容,希望文章能够帮你解决iPhone – 如何在表格单元格上添加可点击按钮?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053613.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存