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



我一直在尝试解析数组并将其拆分为单独的字符串。我已经通过在设置字符串时单独添加固定objectAtIndex来成功地使其工作。

例:

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

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

查询:

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,因为这会引发致命错误。

相关内容

最新更新