![](/img/trans.png)
[英]Get the indexpath from custom button in accessoryView of a UITableViewCell
[英]Get a custom identifier of a custom UITableViewCell from a button press
我有一个使用自定义UITableViewCell
和自定义UIButton
的应用程序。 这些名称是CustomCell
和CustomButton
。
CustomButton
和CustomCell
具有标识符。 CustomButton
具有buttonIdentifier
, CustomCell
具有cellIdentifier
。 创建表时,它们都获得相同的ID。 单元格还有另一个名为projectFullName
自定义属性。
例:
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搞砸了,它不再起作用了。
在你的cellForRowAtIndexPath中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[[customCell button] setTag:indexPath.row];
return cell;
}
单击按钮时获取文本标签的单元格和文本:
UITableViewCell cell = [(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath: [NSIndexPath indexPathForRow:button.tag inSection:0]];
NSString *text = [cell yourLabel].text;
当然,您必须在uitableviewcell类中将按钮和标签定义为属性。
我现在设法做了一个自己的解决方案。 但这似乎不对。 我正在使用for循环迭代我的UITableView
每个CustomCell
。 但是当我有1000个条目时,这可能是性能问题不是吗?
stringEditIdentifier = ((CustomButton *)sender).projectButtonIdentifier;
NSString *labelCellText = @"";
for(int i = 0; i < [tableView numberOfRowsInSection:0]; i++)
{
NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:index];
NSString *textFieldVal = [cell.textLabel text];
NSString *cellIdentifier = cell.projectCellIdentifier;
if ( [cellIdentifier isEqualToString:stringEditIdentifier ] ){
labelCellText = textFieldVal;
}
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Projektname"
message:@""
delegate:self
cancelButtonTitle:@"Abbrechen"
otherButtonTitles:@"Speichern", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView textFieldAtIndex:0].text = labelCellText;
[alertView show];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.