Маленький фон, вид на стол заполнен элементом fetchedResultsController, который заполняет представление таблицы строками. Теперь я пытаюсь добавить кнопку рядом с каждой строкой в каждой ячейке tableview. До сих пор я пытался создать кнопку в моем методе configureCell: atIndexPath, а затем добавить эту кнопку в качестве подчиненного представления содержимого ячейки таблицы, но по какой-то причине кнопки не отображаются. Ниже приведен соответствующий код. Если кто-то хочет добавить больше кода, просто спросите, и я положу все, что захочу. Любая помощь приветствуется.Вопрос iPhone: Как добавить кнопку в ячейку tableview?
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// get object that has the string that will be put in the table cell
Task *task = [fetchedResultsController objectAtIndexPath:indexPath];
//make button
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[button setTitle:@"Check" forState:UIControlStateNormal];
[button setTitle:@"Checked" forState:UIControlStateHighlighted];
//set the table cell text equal to the object's property
cell.textLabel.text = [task taskName];
//addbutton as a subview
[cell.contentView addSubview:button];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Спасибо за ответ, я изменил кнопку [UIButton buttonWithType] на CGRect buttonFrame = CGRectMake (0, 0, 40, 40); UIButton * button = [[UIButton alloc] initWithFrame: buttonFrame]; , но ничего не появляется. Вы думаете, что это может быть затенено чем-то? Редактировать: Он скрыт, я вижу текст под другим текстом String. Теперь мне просто нужно его переместить. Большое спасибо за помощь. – Jake
'+ buttonWithType:' является правильным инициализатором для 'UIButtons'. Вам просто нужно сделать что-то вроде: '[button setFrame: CGRectMake (0,0,40,40)];' впоследствии –