繁体   English   中英

从表视图将数组解析为单个字符串-Objective-C

[英]Parsing array into individual strings from a table view - Objective-C

我一直试图解析一个数组并将其拆分为单独的字符串。 通过设置字符串,我分别添加了一个固定的objectAtIndex ,从而成功地完成了此工作。

例:

 NSString *weightString = [[[results valueForKey:@"Endurance"] objectAtIndex:7]objectAtIndex:2];

但是,当我尝试使用任何objectAtIndex在tableView didSelectRowAtIndexPath设置字符串时,字符串始终返回null。 我需要能够使用objectAtIndex:indexPath.row单击tableView行。 我无法找出我要去哪里。 随时请求更多代码或查询。

查询:

PFQuery *arrayQuery = [PFQuery queryWithClassName:@"Exercises"]; 
[arrayQuery selectKeys:@[@"Endurance"]];
[arrayQuery orderByAscending:@"ExerciseName"];
[arrayQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) { self.arrayResults = [results valueForKey:@"Endurance"];

的cellForRowAtIndexPath:

- (TableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"list";
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

UILabel *cellTitle;
cell.cellTitle.text = [[browseAllArray objectAtIndex: indexPath.row] objectForKey:@"ExerciseName"];

if (cell == nil) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    //Exercise Bar Label
    cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 220, 60)];
    cellTitle.font = [UIFont fontWithName:@"ethnocentric" size:14];
    cellTitle.textColor = [UIColor blackColor];
    cellTitle.numberOfLines = 2;
    cellTitle.textAlignment = NSTextAlignmentCenter;

    cell.cellTitle.text = [[browseAllArray objectAtIndex: indexPath.row] objectForKey:@"ExerciseName"];
    [self.tableView reloadData];
}
cellTitle.text = [[browseAllArray objectAtIndex:indexPath.row]objectForKey:@"ExerciseName"];
return cell;

我设法通过更改didSelectRowAtIndexPath方法中的字符串来解决我的问题。

而不是使用:

 NSString *weightString = [[[results valueForKey:@"Endurance"] objectAtIndex:indexPath.row]objectAtIndex:2];

我用了:

 NSString *weightString = [[arrayResults objectAtIndex:indexPath.row] objectAtIndex:2];

我决定尝试使用数组而不是结果字符串,因为它不会让我从字符串中提取信息。 我建议按照上述@Larme的步骤进行操作,因为它们肯定会向正确的方向发送信息。

如果人们试图遵循我的步骤,请确保删除valueForKey,因为这会引发致命错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM