我有一个带有许多按钮,文本字段,标签和视图的自定义UITableViewCell。我隐藏并向每个索引路径显示它们所需的位置。但由于某些原因/行不可点击!
我甚至通过代码做了以下事情,以确保如果在IB中出现问题,可以通过这些线修复但没有用。
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.userInteractionEnabled = YES;
我想知道隐藏的观点是否是这里的罪魁祸首?
答案 0 :(得分:6)
试试这个tableView.allowsSelection=YES;
您可以实施-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:
答案 1 :(得分:3)
我在UITableViewCells中也使用了很多不同的UI元素,为什么不尝试子类UITableViewCell并滚动自己的类,将这些项作为单元格的属性?似乎对我有用。
VincentsAwesomeCell.h中的:
@interface VincentsAwesomeCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *FooLabel;
@property (weak, nonatomic) IBOutlet UILabel *BarLabel;
@property (weak, nonatomic) IBOutlet UIImageView *ShimmyIcon;
@property (weak, nonatomic) IBOutlet UILabel *DooWopLabel;
@property (weak, nonatomic) IBOutlet UIImageView *UseIcon;
@property (weak, nonatomic) IBOutlet UILabel *TheForceLabel;
@property (weak, nonatomic) IBOutlet UIImageView *bobSledIcon;
@property (weak, nonatomic) IBOutlet UILabel *WristWatchLabel;
@property (weak, nonatomic) IBOutlet UILabel *MeatLoafLabel;
@property (weak, nonatomic) IBOutlet UILabel *SciFiLabel;
@property (weak, nonatomic) IBOutlet UILabel *CollinderLabel;
@property (weak, nonatomic) IBOutlet UILabel *RhubarbLabel;
@property (weak, nonatomic) IBOutlet UITextField *SuperTextField;
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
@property (strong, nonatomic) IBOutlet UILabel *amountOfAwesomeness;
不要忘记使用:
VincentsAwesomeCell.m中的:
- (void)prepareForReuse {
[super prepareForReuse];
// Always call this!
// Reset custom stuff
}
重置单元格的属性以便重用。
然后在你的TableViewController.m中:
- (void) viewDidLoad
{
[super viewDidLoad];
// Register the custom VincentsAwesomeCell NIB with the tableview
UINib *nib = [UINib nibWithNibName:@"VincentsAwesomeCell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"VincentsAwesomeCell"];
}
至于
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
我将它用于UIStepper以跟踪按下哪个步进器,例如:
[[cell stepper]setTag:indexPath.row];
[[cell stepper]addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
希望这有帮助!
P.S。
我使用.xib文件来创建自定义单元格的布局。只需将UITableViewCell拖入InterFaceBuilder即可自定义!不要忘记链接属性!
答案 2 :(得分:2)
这是自定义UITableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *lbl = [[UILabel alloc]initWithFrame:cell.frame];
lbl.backgroundColor = [UIColor clearColor];
UIImageView *cellImage = [[UIImageView alloc]initWithImage: [arrayImages objectAtIndex:indexPath.row]];
if (pointRange.location!=NSNotFound) {
cellImage.frame = CGRectMake(25,12.5, 75, 75);
lbl.frame = CGRectMake(0, 0, tableView.frame.size.width, 100);
lbl.font = [UIFont fontWithName:@"PineappleDemo" size:30.0];
}
lbl.textColor =[UIColor colorWithRed:38.0/255.0 green:34.0/255.0 blue:98.0/255.0 alpha:1.0];
lbl.text =[arrayData objectAtIndex:indexPath.row];
for(UIView *v in [cell subviews])
{
if([v isKindOfClass:[UILabel class]]||[v isKindOfClass:[UIImageView class]]||[v isKindOfClass:[UIButton class]])
[v removeFromSuperview];
}
if (index == indexPath.row) {
UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 90, tableView.frame.size.width-200, 100)];
lbl2.backgroundColor = [UIColor clearColor];
lbl2.numberOfLines = 0;
lbl2.text = [arrayOffers objectAtIndex:indexPath.row];
[cell addSubview:lbl2];
[lbl2 release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(tableView.frame.size.width-150, 100, 120, 84);
btn.backgroundColor = [UIColor clearColor];
if (indexPath.row<=2) {
switch (indexPath.row ) {
case 0:
[btn setImage:[UIImage imageNamed:@"lickFB.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(facebook) forControlEvents:UIControlEventTouchUpInside];
break;
case 1:
[btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(twitter) forControlEvents:UIControlEventTouchUpInside];
break;
case 2:
[btn setImage:[UIImage imageNamed:@"rateUs.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(rateAction) forControlEvents:UIControlEventTouchUpInside];
break;
default:
break;
}
}
else{
[btn setImage:[UIImage imageNamed:@"Buy_Button.png"] forState:UIControlStateNormal];
switch (indexPath.row) {
case 3:
[btn addTarget:self action:@selector(deal1) forControlEvents:UIControlEventTouchUpInside];
break;
case 4:
[btn addTarget:self action:@selector(deal2) forControlEvents:UIControlEventTouchUpInside];
break;
case 5:
[btn addTarget:self action:@selector(deal3) forControlEvents:UIControlEventTouchUpInside];
break;
case 6:
[btn addTarget:self action:@selector(deal4) forControlEvents:UIControlEventTouchUpInside];
break;
default:
break;
}
}
[cell addSubview:btn];
}
[cell addSubview:lbl];
[cell addSubview:cellImage];
[cellImage release];
[lbl release];
return cell;
}
答案 3 :(得分:1)
如果您的表格视图是可编辑的,这可能是单元格无法点击的原因
如果是这种情况,并且您在编辑时确实需要单元格可点击 - 只需执行以下操作:
tableView.allowsSelectionDuringEditing = YES;
答案 4 :(得分:1)
在您的自定义UITableViewCell中,您是否向contentView添加了btn,textfields等?
也许如果你将所有内容添加到contentView,那应该可以解决你的问题。 希望它有所帮助。
答案 5 :(得分:0)
实施此方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
答案 6 :(得分:0)
如果您无法正确触发“didSelectRowAtIndexPath”,请将BIG UIButton占用整个单元格,并在按钮单击时执行必要的操作。只是一个想法! : - )
答案 7 :(得分:0)
我有类似的问题。解决了它,但添加了
self.clipsToBounds = YES;
到我的tableView所在的视图。所以我确定,我的tableView超出了其父视图的框架。