gpt4 book ai didi

iphone - 从 UITableView 中的选定单元格传递数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:16 26 4
gpt4 key购买 nike

我有一个表格 View ,其中每个单元格都填充了数组中的动态数据。从这里开始,当用户选择一行时,我想将特定数据从该单元格推送到 ViewController,以便它最终可以用于 POST 请求。现在我知道我的 tableviewcontroller 上的这段代码只会显示数据

- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
DetailGameController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];
NSUInteger row = [indexPath row];
detail.rowNum.text = [photoTitles objectAtIndex:row];

}

但我如何实际使用这些数据?

这是我所拥有的片段(实际代码有多个 NSMutableArrays 但我只包含一个):

     - (void)viewDidLoad
{

[super viewDidLoad];
photoTitles = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://localhost/test.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:dataCenter.data forKey:@"name"];
[request setDelegate:self];
[request startAsynchronous];

}
- (void)requestFinished:(ASIFormDataRequest *)request
{

NSString *responseString = [request responseString];

NSDictionary *dictionary = [responseString JSONValue];
NSArray *photos = [[dictionary objectForKey:@"photos"] objectForKey:@"photo"];

for (NSDictionary *photo in photo) {
NSString *photo = [photo objectForKey:@"photo"];
[photoTitles addObject:photo];
}
}

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [photoTitles count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSUInteger const kPhotoTitleTag = 2;
UILabel *photoTitleLabel = nil;

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if(cell == nil){


cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SimpleTableIdentifier];

photoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 170, 14)];
[cell.contentView addSubview:photoTitleLabel];
}else {
photoTitleLabel = (UILabel *)[cell.contentView viewWithTag:kPhotoTitleTag];
}
NSUInteger row = [indexPath row];

photoTitleLabel.text = [photoTitles objectAtIndex:row];
return cell;

如果能帮助我朝着正确的方向前进,那就太棒了。如果有任何不清楚的地方,请告诉我。

最佳答案

为什么要传递行的索引?
传递其他 Controller 需要使用的信息。

DetailGameController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
NSUInteger row = [indexPath row];
detail.someProperty = [photoTitles objectAtIndex:row];
detail.someOtherProperty = // other interesting elements ;

关于iphone - 从 UITableView 中的选定单元格传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640424/

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