Ios 检测是否轻触包含多个部分的UITableView的UITableViewCell中的按钮
我想在父UITableView包含多个部分的UITableView单元格上检测按钮点击 我可以在单节的情况下这样做,但在有多个节的情况下,我很难确定正确的节 我试图确定用户点击按钮的UITableViewCell对应的正确部分 以下是问题解决后的更新代码:Ios 检测是否轻触包含多个部分的UITableView的UITableViewCell中的按钮,ios,objective-c,iphone,swift,uitableview,Ios,Objective C,Iphone,Swift,Uitableview,我想在父UITableView包含多个部分的UITableView单元格上检测按钮点击 我可以在单节的情况下这样做,但在有多个节的情况下,我很难确定正确的节 我试图确定用户点击按钮的UITableViewCell对应的正确部分 以下是问题解决后的更新代码: @property (strong, nonatomic) NSMutableArray* quantityArray; @property (strong, nonatomic) NSMutableArray* rows; @synth
@property (strong, nonatomic) NSMutableArray* quantityArray;
@property (strong, nonatomic) NSMutableArray* rows;
@synthesize quantityArray,rows;
- (void)viewDidLoad {
[super viewDidLoad];
quantityArray = [[NSMutableArray alloc] init];
[self PluMinusFunction];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
// Some code here.
//
if (addBtnClicked || minusBtnClicked) {
cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
addBtnClicked = NO;
minusBtnClicked = NO;
} else {
cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
}
cell.btnMinus.tag = indexPath.row;
cell.btnPlus.tag = indexPath.row;
cell.lblCount.tag = indexPath.row;
[cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
if ([ cell.lblCount.text integerValue]!=0) {
[cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
}
#pragma mark - Plus Minus Button
- (void)PluMinusFunction {
for (int j=0; j <[itemArr count]; j++) {
rows = [[NSMutableArray alloc] init];
for (int i=0; i <[ [[itemArr objectAtIndex:j] objectForKey:@"itemList"]count]; i++) {
[rows insertObject:@"0" atIndex:i];
}
[quantityArray insertObject:rows atIndex:j];
}
[self sum];
}
#pragma mark - UIButton selector
- (void)addItem:(UIButton*)button {
CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
NSInteger row = clickedButtonIndexPath.row;
NSInteger section = clickedButtonIndexPath.section;
NSInteger LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] + 1;
NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
[subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];
[quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
addBtnClicked = YES;
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
- (void)deleteItem:(UIButton*)button {
CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
NSInteger row = clickedButtonIndexPath.row;
NSInteger section = clickedButtonIndexPath.section;
NSInteger LabelValue =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue];
if (LabelValue >= 1) {
NSInteger LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] - 1;
NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
[subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];
[quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
addBtnClicked = YES;
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
}
@property(强,非原子)NSMutableArray*quantityArray;
@属性(强,非原子)NSMutableArray*行;
@合成数量数组,行;
-(无效)viewDidLoad{
[超级视图下载];
quantityArray=[[NSMutableArray alloc]init];
[自注入功能];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//
//这里有一些代码。
//
如果(添加勾选| |减少勾选){
cell.lblCount.text=[[quantityArray objectAtIndex:indexath.section-1]objectAtIndex:indexath.row];
addBtnClicked=否;
minusBtnClicked=否;
}否则{
cell.lblCount.text=[[quantityArray objectAtIndex:indexath.section-1]objectAtIndex:indexath.row];
}
cell.btnMinus.tag=indexPath.row;
cell.btnPlus.tag=indexPath.row;
cell.lblCount.tag=indexPath.row;
[cell.btnPlus addTarget:self action:@selector(addItem:)for controlEvents:UIControlEventTouchUpInside];
如果([cell.lblCount.text integerValue]!=0){
[cell.btnMinus addTarget:self action:@selector(deleteItem:)for controlEvents:UIControlEventTouchUpInside];
}
返回单元;
}
}
#pragma标记-加减按钮
-(void)PluMinusFunction{
对于(int j=0;jObjective-C
-(void)addItem:(UIButton*) sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:mainTable]; // maintable --> replace your tableview name
NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint:touchPoint];
NSLog(@"index path.section ==%ld",(long)clickedButtonIndexPath.section);
NSLog(@"index path.row ==%ld",(long)clickedButtonIndexPath.row);
}
Swift3
func addItem(sender: UIButton)
{
var touchPoint = sender.convert(CGPoint.zero, to: self.maintable)
// maintable --> replace your tableview name
var clickedButtonIndexPath = maintable.indexPathForRow(at: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
}
Swift2及以上版本
func addItem(sender: UIButton)
{
var touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: mainTable)
// maintable --> replace your tableview name
var clickedButtonIndexPath: NSIndexPath = mainTable(forRowAtPoint: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
}
在不使用CGPoint
和假设用户界面的情况下,获取索引路径的更直接的方法是查看.superview
。我认为这在实践中更可靠
- (myTableViewCell *)cellContainingView:(UIView *)view
{
do {
view = view.superview;
} while (view != nil && ![view isKindOfClass:[myTableViewCell class]]);
return (myTableViewCell *)view;
}
然后,您只需在按钮操作中执行此操作:
- (IBAction)myButtonPressed:(id)sender
{
myTableViewCell *cell = [self cellContainingView:(UIView *)sender];
NSIndexPath *path = [self.tableView indexPathForCell:cell];
}
斯威夫特5
let touchPoint: CGPoint = sender.convert(.zero, to: tableViews)
let clickedButtonIndexPath = self.tableViews.indexPathForRow(at: touchPoint)
if(clickedButtonIndexPath != nil)
{
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
}
我得到了一个问题>在某些情况下,任何想法ab都是在随机场景中发生的,你面对哪种场景?你能检查一下这个问题吗?Swift代码是让touchPoint:CGPoint=sender.convertPoint(CGPointZero,toView:self.tableView)让buttonIndexPath=self.tableView.indexPathForRowAtPoint(touchPoint)!Swift 3.1-变量clickedButtonIndexPath=mainTable.indexPathForRow(在:接触点)
let touchPoint: CGPoint = sender.convert(.zero, to: tableViews)
let clickedButtonIndexPath = self.tableViews.indexPathForRow(at: touchPoint)
if(clickedButtonIndexPath != nil)
{
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
}