iOS, alertView для определенного элемента в пользовательской ячейке - PullRequest
0 голосов
/ 20 марта 2012

У меня есть пользовательская ячейка для таблицы, работает нормально,

Я показываю список продуктов в ячейках, когда пользователь нажимает кнопку «удалить», я показываю предупреждение, чтобы подтвердить удаление,

но мне нужно показать название продукта в окне предупреждения: "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

Большое спасибо!

1 Ответ

1 голос
/ 20 марта 2012

строковые литералы - ваш друг.

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

Вероятно, лучше переместить это из вашего подкласса ячеек в ваш источник данных и делегировать, чтобы облегчить более чистый код.

...