我在有一个按钮(红色交叉)UITableViewCell,并在该按钮,我想获得的点击indexPath的UITableViewCell.
现在我正在为每个按钮分配标签,这样
cell.closeButton.tag = indexPath.section
按下按钮我得到的indexPath.section值如下:
@IBAction func closeImageButtonPressed(sender: AnyObject) {
data.removeAtIndex(sender.tag)
tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
这是正确的实施方式还是有其他干净的方法来做到这一点?
我目前在单元格中定义了一个按钮,以及跟踪其UITouchDown操作的方法,如下所示:
- (void) clickedCallSign:(id)sender {
int index = [sender tag];
NSLog(@"event triggered %@",index);
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//Callsign button
UIButton *button;
CGRect rect = CGRectMake(TEXT_OFFSET_X, BORDER_WIDTH, LABEL_WIDTH, LABEL_HEIGHT);
button = [[UIButton alloc] initWithFrame:rect];
cell.tag=[indexPath row];
button.tag=[indexPath row];
[button addTarget:self action:@selector(clickedCallSign:) forControlEvents:UIControlEventTouchDown];
[button setBackgroundColor:[UIColor redColor]];
[button setTitle:@"hello" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button release];
}
Run Code Online (Sandbox Code Playgroud)
但是,当我单击模拟器中的单元格时,控制台调试消息是:"event triggered(null)",我的应用程序很快就崩溃了.
如何正确获取我的clickedCallSign方法中的indexPath.row值?
我在自定义单元格中有一个UIButton,我想在用户按下该按钮时触发一个动作但是我需要知道按下UIButton的哪一行.
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
...
[button addTarget:self action:@selector(buttonPressedAction:)forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressedAction:(UIButton *)button
{
//int row = indexPath.row;
//NSLog(@"ROW: %i",row);
}
Run Code Online (Sandbox Code Playgroud)
如何将indexPath作为参数传递给我的选择器?
我有一个自定义方法来检测单元格图像上的点击.我还想找到图像的相关单元格的索引路径,并在函数中使用它.这是我正在使用的:
的cellForRowAtIndexPath:
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
Run Code Online (Sandbox Code Playgroud)
方法我试图获取索引路径:
-(void)cellImageTapped:(id)sender {
if(videoArray.count > 0){
Video *currentVideo = [videoArray objectAtIndex:INDEX_PATH_OF_CELL_IMAGE];
//do some stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何传递索引路径.有任何想法吗?
ios ×3
objective-c ×3
uitableview ×3
cocoa-touch ×2
iphone ×2
uibutton ×2
ipad ×1
selector ×1
swift2 ×1