Как распознать UITableView Cell Touchup внутри более 5 секунд от пользователя
Я создаю одно приложение, где я показываю некоторый rss-контент из URL для моего UITableView, пока он здесь не работает идеально. Но здесь я хочу, чтобы пользователь нажимал и удерживал UITableViewCell более 5 секунд для выполнения других функций. я просто хочу, как узнать один выбор и нажмите и удерживайте выделение для UITableViewCell.
Спасибо
1 ответ
Вы должны добавить UILongPressGestureReconizer в свою ячейку и установить его minimumPressDuration
до 5
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 5.0;
[cell addGestureRecognizer:longPress];
}
// Do additional setup
return cell;
}
РЕДАКТИРОВАТЬ:
Обратите внимание, что если вы используете prototypeCells
, !cell
условие никогда не будет истинным, поэтому вы должны написать что-то вроде:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
if (cell.gestureRecognizers.count == 0) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 5.0;
[cell addGestureRecognizer:longPress];
}
return cell;
}