iOS,自定义单元格中特定元素的alertView

标签 ios uitableview cell uialertview

我有一个用于表格的自定义单元格,工作正常,

我在单元格中显示产品列表,当用户触摸“删除”按钮时,我会显示一个警报 View 以确认删除,

但我需要在警报 View 中显示产品名称:“您确定要删除 XXX 吗?”

这是我的自定义单元格的代码

请注意对 deleteCartButtonPressed 中警报 View 的调用

#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

非常感谢!

最佳答案

字符串文字是你的 friend 。

- (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];
    }

最好将其从单元格子类中移出并移入数据源和委托(delegate)中,以便使代码更简洁。

关于iOS,自定义单元格中特定元素的alertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780030/

相关文章:

ios - 如何在 iOS/Swift 项目中清晰分离 ui 和业务逻辑

javascript - Syncfusion 控件未在 iOS native UIWebView 上加载

ios - UITableView 无法使用 Swift 3 中的 Index 正确显示数据

ios - TableView数据未加载

.net - 如何在 DataGridView 中选择多个单元格

c# - Unity XMPP 聊天

ios - 如何在另一个 React 组件中正确使用 TabNavigator

swift - 如何打印与所选复选标记关联的单元格文本?

Java:GridLayout 单元格似乎合并

swift - 专门更改填充有数组元素的 TableView 中的一个单元格