我有一个 tableview 单元格
。其中有一个按钮
和一个标签
。当用户单击按钮时,我想弹出一个消息框,用户可以在其中键入一些文本并提交。在那种情况下(当用户键入文本并提交时)用户键入的文本应该显示在该单元格的 UILabel
(在那个特定的 cell
中)。我该怎么做?
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.) 如何用文本填充单元格中的标签? (请考虑我已经有了文本)
最佳答案
在按钮操作中将警报 View 的标签设置为
-(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;
}
}
}
}
希望对你有所帮助。
关于ios - 用文本填充单元格中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823497/