【发布时间】:2012-09-15 13:45:21
【问题描述】:
我在表格视图单元格中动态添加一个按钮。按钮显示在桌子上,但我无法点击它们。这是我的代码,
这是我的 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;
}
请帮帮我。 谢谢。
【问题讨论】:
-
每个视图上都有这个按钮吗?渲染是否正确?
-
是的,如果条件为真,那么按钮将显示在视图中。按钮显示正确,但我无法在点击时调用方法。
-
您可能还应该显示用于创建表格单元格的代码。此外,当您单击按钮单元格时会发生什么?有没有崩溃?还是完全没有效果?
-
你能把这段代码放在你的问题中吗?
-
为什么要像附件一样添加按钮?你应该将你的按钮作为一个简单的子视图直接添加到你的单元格中,比如
[cell addSubview:chat_pic_btn];...你已经尝试过这种方式了吗?
标签: iphone ios xcode uitableview uibutton