Я хотел бы изменить UIImage
, который находится внутри UITableViewCell
, когда пользователь выбирает строку. То теперь слишком много. Я могу сделать это в didSelectRowAtIndex
просто отлично. Однако, и проблема заключается в том, что я хочу, чтобы другие ячейки (каждый cell
имел стандартное изображение), чтобы снова иметь стандартное изображение. (Если ячейки снова имеют стандартное изображение, выбранное изображение должно быть «отмечено» (это означает, что немаркированное изображение будет заменено отмеченным изображением, для которого у меня уже есть код)UITableView изменить изображение на ячейку выбрать и сбросить другие
См. Его как Checkbox
что вы знаете из HTML
Вот моя попытка:.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
//Little flashing animation
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[UIView animateWithDuration:2.0f animations:^{
} completion:^(BOOL finished) {
;
}];
//Mark the selected cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ALog(@"%i", cell.imageView.tag);
ALog(@"%i", [[[tableView cellForRowAtIndexPath:indexPath] imageView] tag]);
UIImageView *iconimgView = (UIImageView *)[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:TAG_IMAGEMARKERCELL];
[iconimgView setImage:[UIImage imageNamed:@"circle-answer-marked.png"]];
//..
}
UIImageView
определяется здесь:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//Add this to cell only once, not everytime a cell is displayed! (Everyhing goes in here)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont fontWithName:@"PTSans-Regular" size:33/2]];
[[cell textLabel] setTextColor:[self colorFromHexString:COLOR_DEFAULT_TEXT_COLOR]];
cell.indentationLevel = cell.indentationLevel + 2;
// //Marker image
imgCell_Marker = [[UIImageView alloc]initWithFrame:CGRectMake(10, 22, 20, 20)];
imgCell_Marker.image=[UIImage imageNamed:@"circle-answer.png"];
imgCell_Marker.tag = TAG_IMAGEMARKERCELL;
ALog(@"%@", imgCell_Marker);
[cell.contentView addSubview:imgCell_Marker];
}
//..
return cell;
}
Нечетная вещь, у UIImageView
s есть предопределенный tag
, но если я хочу получить это в didSelectRowAtIndexPath
, ее всегда «0». (значение по умолчанию tag
)
Заранее спасибо.
Спасибо за подсказку с помощью ALOG. Я знаю, что получил правильное значение от тега. – paskl
Почему голос? – Wain
Вам необходимо убедиться, что изображение всегда установлено на правильное значение. В настоящее время повторное использование ячеек разбивает ваш код. Вам необходимо пересмотреть свой подход и обновить модель данных. – Wain