Ios 用文本填充单元格中的标签
我有一个Ios 用文本填充单元格中的标签,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个tableview单元格。里面有一个按钮和一个标签。当用户单击按钮时,我想弹出一个消息框,用户可以在其中键入一些文本并提交。在这种情况下(当用户键入文本并提交时),用户键入的文本应显示在该单元格的ui标签(在该特定的单元格)上。我该怎么做 cell.cellButton.tag = indexPath.row; [cell.cellButton addTarget:self action:@selector(cellButtonClicked:) forControlEv
tableview单元格
。里面有一个按钮
和一个标签
。当用户单击按钮时,我想弹出一个消息框,用户可以在其中键入一些文本并提交。在这种情况下(当用户键入文本并提交时),用户键入的文本应显示在该单元格的ui标签
(在该特定的单元格
)上。我该怎么做
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
-(void)cellButtonClicked: (id)sender {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Enter your text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok"];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
注意:这是每个单元格的外观
问题:
1.)如何用文本填充单元格中的标签?(请考虑我已经有了这篇文章) 你尝试这个代码< /p>
-(void)cellButtonClicked: (id)sender {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Enter your text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok"];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = sender.tag;
[alert show];
}
警报视图确定按钮单击代码
获取TextField
text,并使用indepath
-(void)alertView:(UIAlertView *)alertView1 clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==alertView1.firstOtherButtonIndex) {
NSIndexPath *theIndexPath=[NSIndexPath indexPathForRow:alertView1.tag inSection:0];
UITableViewCell *cell = [self.yourTableViewName cellForRowAtIndexPath:theIndexPath];
cell.yourLabel.text = [alertView1 textFieldAtIndex:0];
[self.tableView reloadRowsAtIndexPaths:theIndexPath withRowAnimation:UITableViewRowAnimationNone];
}
}
将按钮操作中警报视图的标记设置为
-(void)cellButtonClicked: (id)sender {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Enter your text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = sender.tag;
[alert show];
}
之后,在alert view delegate中编写以下代码
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
// Insert whatever needs to be done with "name"
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
UITableViewCell *cell = [_myTableview cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
if([view isKindOfClass:[UILabel class]]){
UILabel *label = (UILabel*) view;
label.text = name;
}
}
}
}
希望它能对您有所帮助。实现表视图的最佳实践是拥有适当的数据源
因此,如果所有单元格都从一个公共位置拾取文本,比如说一个可变数组,那么您可以更新该数组中特定indexath.row上的文本并重新加载表。这太宽了。你在这方面需要什么帮助?您知道如何从警报视图中获取文本吗?您知道如何用文本更新数据模型吗?您知道如何重新加载表格单元格吗?我将创建UIAlertView和UIAlertViewDelegate类别的子类,它告诉我用户何时编辑某些文本并提交。@antonio081014为什么?没有必要
UIAlertView
已经有了所需的内容。@Illep您知道如何在正常情况下将文本放入单元格中。这也不例外。将新文本放入数据模型并重新加载行。@rmaddy但UIAlertView如何将文本传递给单元格或UITableViewController,我想最好在此处提及必要时也更新数据模型。没有理由获取单元格并直接更新其文本。事实上,这是一个糟糕的方法。只需更新数据模型并调用reloadRowsAtIndexPath
。让cellforrowatinexpath
方法根据需要从更新的数据模型中设置单元格的文本。而if
语句应该是if(buttonIndex==alertView1.firstOtherButtonIndex)
。我的建议不太容易出错。也可以更清楚地查看您要检查的按钮。在您的代码中,0
代表什么?是“取消”按钮还是“确定”按钮?1)为什么要深入单元格的子视图并更新每个标签?cell类肯定有标签的属性。2) 一旦用户滚动表视图,此解决方案就会失败。你需要更新数据模型。我听说苹果拒绝使用带有文本框的UIAlertView。我想知道这是不是真的?对不起,我不太清楚拒绝的事情。检查这个链接,它可能会帮助你。