gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:03 24 4
gpt4 key购买 nike

我有这个可折叠的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com