ios - 如何将相关的 TextField 文本数据发送到可折叠 TableView 中的函数?

标签 ios iphone objective-c uitableview uibutton

我有这个可折叠的 UITableView,其中每个表格部分的最后一个单元格中有一个 UITextField 和一个 UIButton。我想将 UITextField 中的文本发送到由同一单元格中它旁边的 UIButton 调用的函数,但我对如何做到这一点。在此先感谢您的帮助!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    if ([self tableView:_tableView canCollapseSection:indexPath.section])
    {
        int num = 1;
        if (self.chat[indexPath.section - 1][@"num"] != nil)
            num = [self.chat[indexPath.section - 1][@"num"] intValue] + 1;

        if (!indexPath.row)
        {
            cell.textLabel.text = self.chat[indexPath.section - 1][@"msg"]; // only top row showing

            if ([expandedSections containsIndex:indexPath.section])
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
            }
            else
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
            }
        }
        else if (indexPath.row < num && indexPath.row >= 1)
        {
            cell.textLabel.text = self.chat[indexPath.section - 1][key];
            cell.accessoryView = nil;
        }
        else
        {
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
            UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];

            self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
            [self.sendButton setTitle:@"Reply" forState:UIControlStateNormal];
            [self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
            [cell addSubview:self.sendButton];

            [self.sendButton addTarget:self action:@selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];

            [cell addSubview:field];
            cell.accessoryView = nil;


        }
    }
    else
    {
        cell.accessoryView = nil;
        cell.textLabel.text = @"Normal Cell";

    }

    return cell;
}

最佳答案

通过以下方式给文本框和按钮一些独特的标签:

//////////////////////////////////////// ////////这是重要的部分///////// /////////////////////////////////////////////

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];

    self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
    [self.sendButton setTitle:@"Reply" forState:UIControlStateNormal];
    [self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
    self.sendButton.tag = indexPath.section *1000 + indexPath.row;
    [cell addSubview:self.sendButton];

    [self.sendButton addTarget:self action:@selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];

    field.tag = self.sendButton.tag + 1;
    [cell addSubview:field];
    cell.accessoryView = nil;

现在按钮事件,

-(void) sendReply:(UIButton *)sender
{
UITextField *field = [YOOUR_TABLE_VIEW viewWithTag:sender.tag + 1];
//Do you coding here
}

关于ios - 如何将相关的 TextField 文本数据发送到可折叠 TableView 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357765/

相关文章:

ios - 无法构建模块 Darwin - arm64 问题

iphone - 根据输入字符串获取CoreData对象数据类型

ios - 从下到上移动对象

iphone - 如何从 iOS 中的 FTP URL 在 ImageView 上加载图像

ios - 使用 KVO 更改属性时发送通知

objective-c - 以编程方式设置后 NSView 框架恢复

ios - 验证 App Store 收据 : Local vs Server

iOS 独立 App 300ms 点击延迟

iphone - 如何将 SVN 修订号添加到 iPhone 应用程序构建?

Objective-C 撤消管理器问题