iphone SDK: Как добавить кнопку в uitableviewCell? - PullRequest
0 голосов
/ 09 октября 2010

Я пытаюсь добавить кнопку в uitableviewcell, и я просто не могу заставить ее работать какие-либо предложения, что я делаю неправильно ??

checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[checkButton setFrame:CGRectMake(0, 0, 30, 30)];
[checkButton setBackgroundImage:[[UIImage imageNamed:@"checked.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[checkButton setBackgroundImage:[[UIImage imageNamed:@"checked.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateSelected];

- (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];
    [cell addSubview:checkButton];

}
NSArray *tempArray = [[exerciseNames objectAtIndex:indexPath.section] objectForKey:@"rowValues"];
[cell addSubview:checkButton];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", [[tempArray objectAtIndex:indexPath.row] objectForKey:@"full"], [[tempArray objectAtIndex:indexPath.row] objectForKey:@"short"]];

// Set up the cell...

return cell;
}

Ответы [ 2 ]

2 голосов
/ 09 октября 2010
- (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];
}
UIButton* checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[checkButton setFrame:CGRectMake(0, 0, 30, 30)];
[checkButton setBackgroundImage:[[UIImage imageNamed:@"checked.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[checkButton setBackgroundImage:[[UIImage imageNamed:@"checked.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateSelected];
[cell addSubview:checkButton];

[checkButton release];

NSArray *tempArray = [[exerciseNames objectAtIndex:indexPath.section] objectForKey:@"rowValues"];

cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", [[tempArray  objectAtIndex:indexPath.row] objectForKey:@"full"], [[tempArray objectAtIndex:indexPath.row] objectForKey:@"short"]];

// Set up the cell...

return cell;
}

Таким образом, кнопка добавляется в ячейку и освобождает собственную память, поэтому нет проблем с утечкой памяти

Также вы можете добавить кнопку в просмотре содержимого ячейки, а не в ячейке

например.

[cell.contentView addSubview: showButton];
1 голос
/ 09 октября 2010

Вы должны создать новый экземпляр кнопки для каждой ячейки, вы не можете использовать общий экземпляр. Представление может быть ТОЛЬКО в одном месте одновременно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...