gpt4 book ai didi

ios - 通过按下按钮获取自定义 UITableViewCell 的自定义标识符

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:39 27 4
gpt4 key购买 nike

我有一个使用自定义 UITableViewCell 和自定义 UIButton 的应用。它们的名称是 CustomCellCustomButton
CustomButtonCustomCell 有一个标识符。 CustomButtonbuttonIdentifierCustomCellcellIdentifier。创建表时,它们都获得相同的 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的文本名称。由于 CustomCellCustomButton 具有相同的标识符,我认为获取内容相当容易,但我的应用程序总是崩溃。这是我尝试过的。

先试试

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 总是空的。这是因为我没有设置它吗?如果是,我应该如何正确设置它?我的意思是,值(value)是动态的,因为我永远不知道我会有多少条目!有没有办法通过 cellIdentifierbuttonIdentifier 获取它,因为它们是一样的?

第四次尝试

我现在手动为按钮添加了标签 ID。

editButton.tag = tagCount;
tagCount = tagCount+1;

在我将新行添加到 UITableViewCustomCell 之前,这一直有效。然后 tagCount 搞砸了,它不再起作用了。

最佳答案

我现在设法做了一个自己的解决方案。但这似乎不对。我正在使用 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];

关于ios - 通过按下按钮获取自定义 UITableViewCell 的自定义标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20635503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com