2013-12-17 183 views
3

我有一个应用程序使用自定义UITableViewCell和自定义UIButton。这些名称是CustomCellCustomButton
CustomButtonCustomCell有一个标识符。 CustomButtonbuttonIdentifierCustomCellcellIdentifier。创建表格时,他们都得到相同的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文本的名称。由于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常是是空的。这是因为我没有设置它吗?如果是,我应该如何正确设置它?我的意思是,价值是动态的,因为我永远不知道我会有多少参赛作品!没有办法通过cellIdentifierbuttonIdentifier得到它,因为它们是一样的吗?

第四次尝试

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

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

这工作,直到我添加了一个新行CustomCellUITableView。然后tagCount被搞乱了,它不再工作了。

+0

我不确定我完全理解,但是,你可以给'CustomButton'指定一个包含单元的IBOutlet吗? (在界面构建器或代码中连接,具体取决于您如何创建按钮/单元格组合。) –

+0

我不太确定如果我明白你的意思。您是否尝试将按钮的标签设置为cellForIndexPath中单元格的索引,然后通过获取索引button.tag处的单元格来确定单元格的文本标签? – dehlen

+0

我从来没有与IBOutlet合作过,所以我不认为如果这将是一条路,因为我不知道它。你能给我一个资源,我可以阅读并试用你的意思吗? – NewToObjectiveC

回答

0
在你的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

[[customCell button] setTag:indexPath.row]; 
return cell; 
} 

当按钮被点击获得的细胞和为textLabel的文字:

UITableViewCell cell = [(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath: [NSIndexPath indexPathForRow:button.tag inSection:0]]; 

NSString *text = [cell yourLabel].text; 

当然,你必须定义你的按钮,并在您的标签作为财产你的uitableviewcell类。

+0

嗨。该按钮不在单元格中。这是一个'subView',但我无法通过'cell.button.tag'访问它 - 它是一个自定义的按钮,来自类CustomButton' – NewToObjectiveC

+0

那么你可以用'UITextField * getTextView =()获取按钮和文本字段UITextField *)[cell.contentView viewWithTag:200];'然后只需将textfield的标签设置为200.那么你不会像我在我的回答中提到的那样使用标签,但是你可以访问组件,并且可以继续。 – dehlen

+0

这里的问题是,首先它是'UILabel'而不是'UITextField' - 好的,这里没有真正的问题,但无论如何。另一个是,我不能将标签设置为某种东西,因为我无法设置标签。标签必须是动态的。 – NewToObjectiveC

0

我现在设法做一个自己的解决方案。但这看起来不正确。我正在使用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]; 
+0

你可以为你的uitextfield和你的uibutton添加一个标识符,然后为它们添加一些随机标签。我cellForRowAtIndexPath通过他们的标签获取控件并设置标识符。然后使用标识符来检索文本。 – dehlen

+0

无论如何只要触发操作,如果通过didSelectRowAtIndexPath单击单元格是没有选择? – dehlen

+0

也看到我编辑的答案 – dehlen