这是我的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;}
请帮帮我.
谢谢.
我创建了一个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 – 如何在表格单元格上添加可点击按钮?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)