我有一个UITableViewCell的子类,我在其中添加了4个UILabel和一个PFImageView(app与后端服务的Parse通信)。这是我的子类代码。
DiscoverTableViewCell.h
#import <UIKit/UIKit.h>
@interface DiscoverTableViewCell : UITableViewCell
@property (strong, nonatomic) PFImageView *profilePictureImageView;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *usernameLabel;
@property (strong, nonatomic) UILabel *descriptionLabel;
@property (strong, nonatomic) UILabel *numberOfFollowersLabel;
@end
DiscoverTableViewCell.m
#import "DiscoverTableViewCell.h"
@implementation DiscoverTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.profilePictureImageView = [[PFImageView alloc] initWithFrame:CGRectMake(15, 10, 55, 55)];
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 20)];
self.usernameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 25, 220, 14)];
self.numberOfFollowersLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, 220, 14)];
self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 58, 220, 20)];
self.nameLabel.font = [UIFont systemFontOfSize:15];
self.usernameLabel.font = [UIFont systemFontOfSize:12];
self.usernameLabel.textColor = [UIColor lightGrayColor];
self.descriptionLabel.font = [UIFont systemFontOfSize:12];
self.numberOfFollowersLabel.font = [UIFont systemFontOfSize:12];
self.descriptionLabel.textColor = [UIColor lightGrayColor];
self.numberOfFollowersLabel.textColor = [UIColor lightGrayColor];
self.profilePictureImageView.userInteractionEnabled = YES;
self.nameLabel.userInteractionEnabled = YES;
self.usernameLabel.userInteractionEnabled = YES;
self.descriptionLabel.userInteractionEnabled = YES;
self.numberOfFollowersLabel.userInteractionEnabled = YES;
self.userInteractionEnabled = YES;
[self.contentView addSubview:self.profilePictureImageView];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.usernameLabel];
[self.contentView addSubview:self.descriptionLabel];
[self.contentView addSubview:self.numberOfFollowersLabel];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
这是我对tableView的实现:cellForRowAtIndexPath:method。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"discoverCell";
DiscoverTableViewCell *cell = (DiscoverTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell){
cell = [[DiscoverTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
if (self.searchResults.count > 0){
if (self.searchScopeIndex == 0){
//Display users
PFUser *user = (PFUser *)[self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = [user objectForKey:@"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"@%@", [user objectForKey:@"username"]];
} else if (self.searchScopeIndex == 1){
//Display hashtags
}
}
} else {
PFUser *user = (PFUser *)[self.objects objectAtIndex:indexPath.row];
NSString *name = [user objectForKey:@"name"];
NSString *username = [user objectForKey:@"username"];
NSString *description = [user objectForKey:@"description"];
NSNumber *numberOfFollowers = [user objectForKey:@"followerCount"];
PFFile *profilePicture = [user objectForKey:@"profilePicture"];
cell.profilePictureImageView.file = profilePicture;
[cell.profilePictureImageView loadInBackground];
cell.nameLabel.text = name;
cell.usernameLabel.text = [NSString stringWithFormat:@"@%@", username];
if (![description isEqualToString:@""] && ![description isEqualToString:@"(null)"]){
cell.descriptionLabel.text = description;
}
if ([numberOfFollowers integerValue] == 1){
cell.numberOfFollowersLabel.text = [NSString stringWithFormat:@"%@ follower", numberOfFollowers];
} else {
cell.numberOfFollowersLabel.text = [NSString stringWithFormat:@"%@ followers", numberOfFollowers];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}
return cell;
}
问题是,在iOS设备上运行时,这不允许选择单元格。单元格显示所有信息OK,但从不调用方法tableView:didSelectRowAtIndexPath :. 谢谢。