Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 识别按下按钮的行_Objective C_Uitableview - Fatal编程技术网

Objective c 识别按下按钮的行

Objective c 识别按下按钮的行,objective-c,uitableview,Objective C,Uitableview,每个“btn1”调用相同的方法 注:这是一个UITableView 我想知道如何从按下按钮的那一行获取信息。我尝试在每个按钮中进行循环,并将按钮标记设置为与上一个不同: // Handle the buttons tag int tagCount = 100; // mainArray is the array of the tableView for (int i = 0; i < [self.mainArray count]; i++) { // I created a N

每个“btn1”调用相同的方法


注:这是一个
UITableView

我想知道如何从按下按钮的那一行获取信息。我尝试在每个按钮中进行循环,并将按钮标记设置为与上一个不同:

// Handle the buttons tag
int tagCount = 100;

// mainArray is the array of the tableView
for (int i = 0; i < [self.mainArray count]; i++) {
    // I created a NSIndexPath to access the tableView cells
    NSIndexPath *nowIndex = [NSIndexPath indexPathForRow:i inSection:0];
    UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex];

    // I set the button tag as the 'int';
    UIButton *shareButton = (UIButton *)[cell viewWithTag:100];
    [shareButton setTag:tagCount];

    tagCount++;
}
//处理按钮标记
int标记计数=100;
//mainArray是tableView的数组
for(int i=0;i<[self.main数组计数];i++){
//我创建了一个nsindepath来访问tableView单元格
NSIndexPath*nowIndex=[NSIndexPath indexPathForRow:i第1节:0];
UITableViewCell*单元格=(UITableViewCell*)[(UITableView*)self.view单元格for行索引路径:nowIndex];
//我将按钮标签设置为“int”;
UIButton*shareButton=(UIButton*)[带标记的单元格视图:100];
[shareButton setTag:tagCount];
tagCount++;
}
但是标签仍然等于100


任何解决方案?

如果按钮是
UIButton
实例,则设置标记属性(可能是行号)。在event方法中,您可以测试tag属性以查看按下了哪个按钮。

您可以将
UIButton
实例替换为
UIImageView
,并将其设置为表视图单元格的
accessoryView
。然后实现
UITableViewDelegate
tableView:accessorybuttonattappedforrowwithindexath:
方法。这样,您就不必处理标记,也不必存储对按钮的引用。如果您不需要任何UIButton交互,如选择的状态或其他手势,则此方法应该足够了。

我的想法是:在每行中循环一次,并将buttons标记设置为
(最后一行标记)+1
,但它不起作用。请看一下我刚才添加到问题中的代码。在tableView:cellForRowAtIndexPath中,当您填写单元格并将UIButton设置为accessoryView时,请将该点的标记设置为indexPath.row。并非每行都有一个UITableViewCell。UITableView仅为屏幕上的内容创建足够的单元格。您可以提前(在viewDidLoad中)创建按钮,也可以在cellForRowAtIndexPath中惰性地创建按钮。这是一个好主意:
UIButton*shareButton=(UIButton*)[CellViewWithTag:100];[shareButton setTag:[indexPath行]]
它正常设置标签,但当按下按钮时,
[sender tag]
不正确。这真的很奇怪。好吧,我的解决方案是:我从界面生成器中删除了按钮,完全通过编程实现,并将按钮标记设置为
[indepath行]
,效果非常好。谢谢你的帮助!这是个好主意,但问题是我需要更改
accessoryView
位置。