我几乎是新手来创建一个自定义单元格。我有一个自定义单元格,并在每行上创建了6个标签,并带有自定义按钮(btnEdit
)。自定义按钮从.xib
下拉。 btnEdit
在两个标签上创建一个框架,如果单击该框架,则调用另一个正常工作的函数。
我唯一的问题是,如果我点击行中的btnEdit
之一,我不希望在另一行中单击它,除非它被删除或者如果一个被选中而另一个被点击它删除第一个并构建另一个。
这是我希望它有用的代码;
·H
@interface PositionTableCell : UITableViewCell {
IBOutlet UILabel *lblSymbol;
IBOutlet UILabel *lblSpl;
IBOutlet UILabel *lblLpl;
IBOutlet UILabel *lblrate;
IBOutlet UILabel *lblAmount;
IBOutlet UILabel *lblO;
}
@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, assign) BOOL isRowSelected;
@end
.M
- (IBAction)btnEdit:(id)sender
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(value1)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[lblLpl addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(value2)];
tapGestureRecognizer2.numberOfTapsRequired = 1;
[lblSpl addGestureRecognizer:tapGestureRecognizer2];
if (!isSelected){
lblLpl.layer.borderColor = lblSpl.layer.borderColor = [UIColor blueColor].CGColor;
lblLpl.layer.borderWidth = lblSpl.layer.borderWidth = 5.0;
lblLpl.userInteractionEnabled = lblSpl.userInteractionEnabled= YES;
isRowSelected = YES;
isSelected = YES;
}
else if (isSelected){
lblLpl.layer.borderColor = lblSpl.layer.borderColor = [UIColor clearColor].CGColor;
lblLpl.layer.borderWidth = lblSpl.layer.borderWidth = 0;
lblLpl.userInteractionEnabled = lblSpl.userInteractionEnabled= NO;
isRowSelected = NO;
isSelected = NO;
}
[tapGestureRecognizer release];
[tapGestureRecognizer2 release];
NSLog(@"CLICKED");
}
答案 0 :(得分:0)
可以通过使用您之前选择的单元格的引用来解决它。
在.m文件中声明一个变量,如:
static PositionTableCell *previousCell = nil;
修改你的方法,如:
- (IBAction)btnEdit:(id)sender
{
if (previousCell != nil && previousCell != self)
{
for (id subLabel in [[previousCell contentView] subviews])
{
if ([subLabel isKindOfClass:[UILabel class]])
{
UILabel *tempLabel = (UILabel *)subLabel;
tempLabel.layer.borderColor = [UIColor clearColor].CGColor;
tempLabel.layer.borderWidth = 0;
tempLabel.userInteractionEnabled = NO;
isRowSelected = NO;
isSelected = NO;
}
}
}
// Other codes here
}