У меня есть подклассовый UITableViewCell с кнопкой «Старт» и «Готово» в каждой ячейке. Кнопка «Готово» создается после нажатия «Старт». Если у меня есть моя кнопка, нажмите логику в моем подклассе UITableViewCell файл, это срабатывает просто отлично.Не удается запустить мой UIButton в TableViewCell
Но я понимаю, что я действительно хочу, чтобы кнопка нажала, чтобы вызвать режим редактирования в представлении таблицы, поэтому я хочу иметь некоторую логику в моей cellForRowAtIndexPath вместо подкласса. Но тогда я не могу получить кнопку, чтобы стрелять :(
Что я делаю неправильно или не понимая?
Мой TableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self.startButton = [[UIButton alloc] initWithFrame:CGRectMake(250 + 0.60 * self.screenWidth, 65, 150, 30)];
[self.startButton addTarget:self action:@selector(onStartButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.startButton.backgroundColor = [UIColor colorWithRed:0.16 green:0.65 blue:0.19 alpha:1.0];
btnLayer = [self.startButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:5.0f];
[self.startButton setTag:1];
[self addSubview:self.startButton];
}
- (void)onStartButtonPressed:(id)sender {
if (self.countdown)
{
[self beginDoneButton];
}
}
-(void)beginDoneButton {
self.startButton.enabled = NO;
self.startButton.hidden = YES;
self.doneButton = [[UIButton alloc] initWithFrame:CGRectMake(250 + 0.60 * self.screenWidth, 65, 150, 30)];
self.doneButton.backgroundColor = [UIColor colorWithRed:0.16 green:0.65 blue:0.19 alpha:1.0];
CALayer *btnLayer = [self.doneButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:5.0f];
[self.doneButton setTitle:@"Done "forState:UIControlStateNormal];
[self addSubview:self.doneButton];
}
Мой TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// get index of Data
int index = (int) indexPath.row;
id order = [orders objectAtIndex:index];
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderCell"];
cell = [[OrderTableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"orderCell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.customerNameLabel.text = [order objectForKey:@"first_name"];
cell.orderLabel.text = [NSString stringWithFormat: @"Order #%@", [order objectForKey:@"order_id"]];
[cell.customerImageView sd_setImageWithURL:[NSURL URLWithString: [order objectForKey:@"profile_image"]]
placeholderImage:[UIImage imageNamed:@"stub"]
];
[cell.messageButton setTitle:@"Message" forState:UIControlStateNormal];
// This doesn't work
[cell.doneButton addTarget:self action:@selector(onDoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
// This doesn't get fired
- (IBAction)onDoneButtonPressed:(id)sender {
NSLog(@"pressed");
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *pathToCell = [self.tv indexPathForCell:cell];
}
Ошибка в том, что cell.doneButton равен нулю, когда вы пытаетесь добавить свою цель и действие, поскольку эта кнопка не создается, пока вы не нажмете кнопку «Пуск». – rdelmar
@rdelmar спасибо. вы правы, у вас есть предложение, как я могу это решить? –
Создайте doneButton в initWithStyle: reuseIdentifier затем установите doneButton.hidden = 1. Когда вы нажмете кнопку startButton, отобразите его. – PowHu