ios - 用文本填充单元格中的标签

标签 ios objective-c uitableview

我有一个 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];

}

注意:这是每个单元格的样子 enter image description here

问题:

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/

相关文章:

iphone - 如何检索 iPhone 的区域设置

swift - 试图找到一种更好的方法将 API JSON 映射到不同的结构取决于 swift 中选择的类别

ios - 使用 AVAssetWriter 录制无缝音频

ios - AWS IOT 连接在 IPAD OS v12.1.1 上关闭

javascript - iOS getUserMedia 的 Bowser 未定义

iphone - 画图应用的 OpenGL ES 内容截图

ios - UserDefaults 和 If/else 不稳定的行为

objective-c - 设置不透明 :NO vs setBackgroundColor:[NSColor clearColor]

ios - 将 Firebase 的图像下载到 TableView

ios - 通过点击表格 View 中的单元格来选中/取消选中复选框,以及如何知道哪个单元格已选中或未选中