This topic has been covered over and over, but after two days of researching and trying all of the solutions suggested, I still can’t do what I want.
First of all, I’m creating an app for iOS 5, using storyboard.
I have a UITableViewController, with 2 types of cell (an original “message”, and a number of “answers” to it). I created my table in my storyboard, checked “prototypes cells”, designed 2 cells with my 2 or 3 labels, a textView, and an image. Then, I subclassed UITableViewCell with 2 new classes, which I called ThreadAlertCell and ThreadAnswerCell. I created properties for my cell’s elements, so I can set the text of the labels and the image programmatically. I linked my graphic elements to their definition in the storyboard as usual. In my TableViewController, in cellForRowAtIndexPath, I create the cells and populate them. So far so good, everything is displayed correctly and how I want it.
But, I want a “touch” on the image of a cell, to pop a new view, showing the user’s profile page (an other basic view, I can do it with performSegue, no problem for that).
I have tried so many things I’m not sure it’s very useful to put everything in detail here. While looking for answers, I understood that using a UIImageView when you expect to handle gestures is not really the best way. So I changed it to a UIButton. But I can’t get the touch event to do anything !
I’ll give only the example of an “answer” cell.
Here is my ThreadAnswerCell header file (I won’t give the .m, nothing interesting there) :
@interface ThreadAnswerCell : UITableViewCell
@property (nonatomic) IBOutlet UILabel *senderLabel;
@property (nonatomic) IBOutlet UILabel *dateLabel;
@property (nonatomic) IBOutlet UITextView *contentTextView;
@property (nonatomic, weak) IBOutlet UIButton *senderButton;
@end
And here is half the cellForRowAtIndexPath from my TableViewController (I do the same for the “message” before that) :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ThreadAnswerCell";
ThreadAnswerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[ThreadAnswerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Message *message = [threadArray objectAtIndex:indexPath.row];
cell.senderLabel.text = [NSString stringWithFormat:@"%@", message.sender];
cell.contentTextView.text = [NSString stringWithFormat:@"%@", message.content];
cell.dateLabel.text = [NSString stringWithFormat:@"%@", message.date];
//[cell.senderButton setBackgroundImage: [[UIImage alloc] initWithData: [NSData dataWithBase64EncodedString: message.userPic]]
// forState: UIControlStateNormal];
[cell.senderButton addTarget:self action:@selector(firstButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
CGRect frame = cell.contentTextView.frame;
frame.size.height = cell.contentTextView.contentSize.height;
cell.contentTextView.frame = frame;
[cell.contentTextView sizeToFit];
return cell;
}
As you can see, I use my custom cell, then populate it with content (I use three useless stringWithFormat but I have my reasons, lol), and I try to add an event to my button. I also commented the part where I set the button’s background image to “see” the button on my screen.
And here is the method I want the buttons to call :
- (void)firstButtonSelected: (id)sender
{
NSLog(@"hello");
}
But, the method is never called ! Any ideas on where I’ve gone wrong or any other working solution would be great ! Thanks.
Do you have
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathImplemented in your tableview delegate class?
I think, your cell catches the touch events. Try to delete the method. And
cell.selectionStyle = UITableViewCellSelectionStyleNone