我有一个应用程序使用自定义UITableViewCell
和自定义UIButton
。这些名称是CustomCell
和CustomButton
。
CustomButton
和CustomCell
有一个标识符。 CustomButton
有buttonIdentifier
和CustomCell
有cellIdentifier
。创建表格时,他们都得到相同的ID。单元格还有另一个名为projectFullName
的自定义属性。从按钮获取自定义UITableViewCell的自定义标识按
例子:
NSString *tempString = @"temp"; // The content is always different
button.buttonIdentifier = tempString;
cell.cellIdentifier = tempString;
cell.projectFullName = @"temp"; // The content is always different
现在,我想要做的是,当我按下按钮,我也得到了小区的textLabel
的内容。所以我基本上做的是。
-(void)editProject:(id)sender {
stringEditIdentifier = ((CustomButton *)sender).projectButtonIdentifier;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Projektname"
message:@""
delegate:self
cancelButtonTitle:@"Abbrechen"
otherButtonTitles:@"Speichern", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
但我也想现在要做的就是通过做预填输入框下面
[alertView textFieldAtIndex:0].text = labelCellName;
的labelCellname
是我之前分配textLabel
文本的名称。由于CustomCell
和CustomButton
具有相同的标识符,因此我认为获取内容相当容易,但是我的应用程序总是崩溃。这是我试过的。
首先尝试
CustomButton *buttonTemporary = (CustomButton *)sender;
CustomCell *cellTemporary = (CustomCell *)[buttonTemporary superview];
NSString *labelCellName = cellTemporary.projectFullName;
错误
UITableViewCellScrollView projectFullName]: unrecognized selector sent to instance 0xa895df0
2013-12-17 14:17:09.279 TodolistiOS[17702:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellScrollView projectFullName]: unrecognized selector sent to instance 0xa895df0'
第二次尝试
int row = ((CustomButton *)sender).tag;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:0]; // in case this row in in your first section
CustomCell* cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *labelCellName = cell.projectFullName;
错误
应用程序崩溃没有任何错误。但是,如果我在UITableView
中有三个条目都具有不同的名称labelCellName
始终是第一个条目的名称。所以当我从第二个条目点击CustomButton
时,我仍然得到第一个条目的名称。我究竟做错了什么?我希望我已经提供了足够的信息,因为我的最后一个问题不太好。
第三次尝试
CustomButton *button = (CustomButton*)sender;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:button.tag inSection:0];
CustomCell *cellT = (CustomCell*)[tableView cellForRowAtIndexPath:myIP];
NSString *testTemp = cellT.projectFullName;
NSLog (@"%@" , testTemp);
错误
相同第二尝试
此外,当我做到这一点
CustomButton *button = (CustomButton*)sender;
NSLog (@"%d", button.tag);
的button.tag常是是空的。这是因为我没有设置它吗?如果是,我应该如何正确设置它?我的意思是,价值是动态的,因为我永远不知道我会有多少参赛作品!没有办法通过cellIdentifier
和buttonIdentifier
得到它,因为它们是一样的吗?
第四次尝试
我现在手动添加标签ID的按钮。
editButton.tag = tagCount;
tagCount = tagCount+1;
这工作,直到我添加了一个新行CustomCell
到UITableView
。然后tagCount被搞乱了,它不再工作了。
我不确定我完全理解,但是,你可以给'CustomButton'指定一个包含单元的IBOutlet吗? (在界面构建器或代码中连接,具体取决于您如何创建按钮/单元格组合。) –
我不太确定如果我明白你的意思。您是否尝试将按钮的标签设置为cellForIndexPath中单元格的索引,然后通过获取索引button.tag处的单元格来确定单元格的文本标签? – dehlen
我从来没有与IBOutlet合作过,所以我不认为如果这将是一条路,因为我不知道它。你能给我一个资源,我可以阅读并试用你的意思吗? – NewToObjectiveC