Чтобы прокомментировать предложение UIButton
, вот фрагмент того, что я сделал для UIButton
, который отображается с UIImage
.
Это было для отслеживания определенного UIButton
типа изображения, которое было затронуто в пределах UITableViewCell
, и оно проходит вдоль строки и раздела, в котором было размещено это UIButton
:
UIButton *_infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[_infoButton setImage:[UIImage imageNamed:@"Info.png"] forState:UIControlStateNormal];
[_infoButton setTitle:[NSString stringWithFormat:@"%d:%d", indexPath.section, indexPath.row] forState:UIControlStateReserved];
[_infoButton addTarget:self action:@selector(touchedDetailedInfoButton:) forControlEvents:UIControlEventTouchDown];
// ...
[ _infoButton release];
А вот связанный метод восстановления заголовка и выполнения с ним чего-то интересного:
- (void) touchedDetailedInfoButton:(NSString *)title {
unsigned int _section, _row;
const char * _indexPathCharPtr = [title cStringUsingEncoding:NSUTF8StringEncoding];
sscanf(_indexPathCharPtr, "%d:%d", &_section, &_row);
NSUInteger _path[2] = {_section, _row};
NSIndexPath *_touchedIndexPath = [[NSIndexPath alloc] initWithIndexes:_path length:2];
[self doSomethingInterestingWithIndexPath:_touchedIndexPath];
[_touchedIndexPath release];
}
Вероятно, есть более простой способ сделать это, так что, надеюсь, кто-то другой придет и предложит альтернативу (кроме подкласса UIImageView
, который также является вполне допустимым вариантом).