我有一个桌子的自定义单元格,工作正常,
我在单元格中显示产品列表,当用户触摸“删除”按钮时,我会显示警报视图以确认删除,
但我需要在提醒视图中显示产品名称:"are you sure you want to delete XXX?"
这里是我的自定义单元格的代码
请注意deleteCartButtonPressed
#import "ShoppingCartProductsCell.h"
#import "Product.h"
@implementation ShoppingCartProductsCell
@synthesize categoryNameLabel = _categoryNameLabel;
@synthesize productNameLabel = _productNameLabel;
@synthesize quantityPicker = _quantityPicker;
@synthesize deleteCartButton = _deleteCartButton;
@synthesize product = _product;
- (void) dealloc {
[_deleteCartButton release];
[_categoryNameLabel release];
[_productNameLabel release];
[_quantityPicker release];
[super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
//self = [super initWithFrame:frame];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self initQuantityPicker];
[self initLabels]; //y picker!
[self initButtons];
}
return self;
}
- (void) initQuantityPicker {
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
self.quantityPicker = [[[BDFDropDownList alloc] initWithFrame:CGRectMake(boundsX+220, 8, 61, 28) popupWidth:90]autorelease];
self.quantityPicker.delegate = self;
for (int i = 1; i<=20; i++) {
[self.quantityPicker addOptionWithName:[NSString stringWithFormat:@"%d",i] value:[NSNumber numberWithInt:i]];
}
[self.contentView addSubview:self.quantityPicker];
}
- (void) initLabels {
self.productNameLabel = [[[UILabel alloc]init] autorelease];
self.productNameLabel.textAlignment = UITextAlignmentLeft;
self.productNameLabel.backgroundColor = [UIColor clearColor];
self.productNameLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
self.productNameLabel.textColor = [UIColor colorWithRed:102/255.0f green:102/255.0f blue:102/255.0f alpha:1];
[self.contentView addSubview:self.productNameLabel];
}
- (void) initButtons {
self.deleteCartButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.deleteCartButton addTarget:self action:@selector(deleteCartButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.deleteCartButton setImage:[UIImage imageNamed:@"deleteCartButton.png"] forState:UIControlStateNormal];
[self.contentView addSubview:self.deleteCartButton]; //Calculations For stage 2
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,10, 200, 20);
self.productNameLabel.frame = frame;
frame= CGRectMake(boundsX+330 ,8, 30, 29); //310
self.deleteCartButton.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) setProduct:(Product*)product {
[self setProduct:product withQuantity:0];
}
- (void) setProduct:(Product*)product withQuantity:(NSInteger)quantity {
[_product release];
_product = product;
[_product retain];
self.productNameLabel.text = product.SKU;
self.quantityPicker.delegate = nil;
[self.quantityPicker setSelectedIndex:quantity-1]; //testa
self.quantityPicker.delegate = self;
}
- (void) deleteCartButtonPressed:(id) sender {
NSLog(@"Delete ");
UIAlertView *deleteAlert = [[UIAlertView alloc]initWithTitle:@"Attention" message:@"Are you sure you want to delete this record?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[deleteAlert show];
[deleteAlert release];
}
@end
非常感谢!
答案 0 :(得分:1)
字符串文字是你的朋友。
- (void) deleteCartButtonPressed:(id) sender {
NSLog(@"Delete ");
UIAlertView *deleteAlert = [[UIAlertView alloc]initWithTitle:@"Attention" message:[NSString stringWithFormat:@"Are you sure you want to delete %@?", [myTableDatasource objectAtIndex:idx]], delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[deleteAlert show];
[deleteAlert release];
}
最好将其移出您的单元子类并进入您的数据源并进行委托,以便更清晰的代码。