简体   繁体   中英

How to change one UITableViewCell when it is touched

I have a table view cell with multiple images in them. When touching the images they shold display an overlay on top of the image which tells the user that this image was selected.

Is there a way to change the look of just one UITableViewCell without having to do a [tableView reloadData] which would allow me to style the cell differently in the table view datasource delegate method.

The way I would do it is to subclass UITableViewCell and then on tableView:didSelectRowAtIndexPath: get a reference to the cell and do whatever you want to it (or just target the image touch event if this is not a selection).

There might be another way of doing this without having to subclass, but I find myself subclassing UITableViewCell all the time and it's pretty straightforward to do.

If you wish to avoid subclassing, this can be achieved with gesture recognisers. Your question suggests a Tap and Hold user interaction on each image, which I have implemented in the code below. One point to remember, if the user is tapping and holding, they may not see the text you wish them to see.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"ImageCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}

UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];

 UILongPressGestureRecognizer *recognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Ben.png"]];
imageView.frame = CGRectMake(cell.contentView.bounds.origin.x,cell.contentView.bounds.origin.y , 100, 40);
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:recognizer];
[cell.contentView addSubview:imageView];

UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Steve.png"]];
imageView2.frame = CGRectMake(cell.contentView.bounds.origin.x + imageView.frame.size.width + 10,cell.contentView.bounds.origin.y , 100, 40);
imageView2.userInteractionEnabled = YES;
[imageView2 addGestureRecognizer:recognizer2];
[cell.contentView addSubview:imageView2];

[imageView release];
[imageView2 release];
[recognizer release];
[recognizer2 release];

return cell;}



- (void)imageTapped:(id)sender {
    NSLog(@"%@", sender);

    UILongPressGestureRecognizer *recognizer = (UILongPressGestureRecognizer *)sender;

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        UILabel *label = [[UILabel alloc] initWithFrame:recognizer.view.bounds];
        label.text = @"Pressed";
        label.backgroundColor = [UIColor clearColor];
        label.tag = 99999;
        label.textColor = [UIColor whiteColor];
        [recognizer.view addSubview:label];
        [label release];
    }
    else {
        [[recognizer.view viewWithTag:99999] removeFromSuperview];
    }
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM