我在表格单元格中放了一个自定义按钮。我想在长按单元格时隐藏该按钮,并在单击取消按钮时显示。
编辑之前:
箭头按钮是自定义按钮,长按时想隐藏,点击取消时显示
这里是表委托函数:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"select press");
static NSString *identifier=@"Cell";
UITableViewCell *cellVal=[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cellVal == nil) {
cellVal = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cellVal.layer.cornerRadius = 9;
titleLabel=(UILabel *)[cellVal viewWithTag:3];
locationLabel=(UILabel *)[cellVal viewWithTag:4];
timeLabel=(UILabel *)[cellVal viewWithTag:5];
detailButton=(UIButton *)[cellVal viewWithTag:10];
detailButton.tag=indexPath.row;
if(!btn){
detailButton.hidden=NO;
}
else{detailButton.hidden=YES;
}
titleLabel.text=[name objectAtIndex:indexPath.row];
locationLabel.text=[loc objectAtIndex:indexPath.row];
timeLabel.text=[time objectAtIndex:indexPath.row];
if (userSelectedAll == TRUE) {
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
} else if (userSelectedAll == FALSE) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
return cellVal;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didSelectRowAtIndexPath %ld",(long)indexPath.row);
[arSelectedRows addObject:indexPath];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"did De SelectRowAtIndexPath");
[arSelectedRows removeObject:indexPath];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete)
{
[[DataBaseFile getSharedInstance]delete:[[DataBaseFile getSharedInstance] getDBFilePath] withName:[[reminderList objectAtIndex:indexPath.row] integerValue]];
}
[tableView reloadData];
}
以下是长按代码:
-(void)handleLongPress1:(UILongPressGestureRecognizer *)gestureRecognizer
{
self.cancelOut.hidden=NO;
self.selectAllOut.hidden=NO;
btn=TRUE;
self.tableVW.allowsSelection=NO;
self.tableVW.allowsMultipleSelection=YES;
self.toolBarOut.hidden=NO;
self.tableVW.allowsMultipleSelectionDuringEditing = YES;
[self.tableVW setEditing: YES animated: YES];
}
取消按钮代码:
-(void) cancelTableEditClick: (UIButton *) sender
{
self.cancelOut.hidden=YES;
self.selectAllOut.hidden=YES;
btn=FALSE;
self.tableVW.allowsSelection = NO;
self.toolBarOut.hidden=YES;
[self.tableVW setEditing: NO animated: YES];
[self.tableVW reloadData];
}
答案 0 :(得分:0)
首先,您应该使用YES / NO作为btn
的值,而不是TRUE / FALSE。在Objective-C中,BOOL
变量只有两个值:YES
,NO
。
其次,您可以检查[cellVal viewWithTag:10]
中的代码tableView:cellForRowAtIndexPath:
是否返回nil
?因为您拨打[self.tableVW reloadData]
,这将再次呼叫tableView:cellForRowAtIndexPath:
,您的按钮将被设置为隐藏,您的代码是正确的,因此请检查您是否未按viewWithTag:10
获取按钮。< / p>
答案 1 :(得分:0)
我得到了简单的答案,就这样做,在cellForRowAtIndexPath:委托函数中, 将此自定义按钮设置为Accessoryview,
UIButton *myAccessoryButton = [[UIButton alloc] init];
[myAccessoryButton setFrame:CGRectMake(0, 0, 48.0, 48.0)];
[myAccessoryButton setImage:[UIImage imageNamed:@"arrow_button.png"] forState:UIControlStateNormal];
[myAccessoryButton addTarget:self action:@selector(detailBtn:) forControlEvents:UIControlEventTouchUpInside];
[cellVal setAccessoryView:myAccessoryButton];
然后是详细功能,
- (IBAction)detailBtn:(UIButton*)sender {
NSLog(@"Detail btn clicked.... ");
}
现在你可以在长按
时隐藏按钮