0

我在下一个 segue 中填充 tableview 时遇到问题。基本上,我将所有字符串发送到一个组合字符串中,然后用它填充一个数组。我正在从该数组中填充表格。问题是这仅出现在第一个单元格中。我希望每个有逗号的条目都换到下一个单元格。这是第一个 ViewControler.h 的代码

@interface ViewController : UIViewController {

IBOutlet    UITableView     *myTableView;
NSMutableArray  *locationArray;

}


@property (nonatomic, retain) IBOutlet  UITableView     *myTableView;
@property (nonatomic, retain)           NSMutableArray  *locationArray;

- (void) populateLocationArray;
@end

这是 ViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
 NSLog(@"Row Selected %i",indexPath.row);



if (indexPath.section==0) {

    [self performSegueWithIdentifier:@"first" sender:nil];

}

}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(@"PrepareForSegueCalled");
LocationItems *destination = [segue destinationViewController];

//If statement for city pressed
destination.titleOfView = @"Boston";

NSArray *myStrings = [[NSArray alloc] initWithObjects:@"first", @"second", @"third", @"fourth", @"fifth", nil];
destination.arrayToBePopulatedString = [myStrings componentsJoinedByString:@" "];
// destination.arrayToBePopulatedString = [myStrings componentsJoinedByString:@"|"];
   // [destination.populationArray addObjectsFromArray:myStrings];
}

名为 LocationItems 的控制器是包含我要填充的表的视图。这是 LocationItems.h

@interface LocationItems : UIViewController
{
IBOutlet    UITableView     *locationTableView;
NSString *titleOfView;
NSString *arrayToBePopulatedString;
NSMutableArray *populationArray;

NSMutableArray *bostonArray;
}

- (void) populateArray;

@property (nonatomic, retain) IBOutlet UITableView *locationTableView;
@property (strong, nonatomic) NSString *titleOfView;
@property (strong, nonatomic) NSString *arrayToBePopulatedString;
@property (strong, nonatomic) NSMutableArray *populationArray;
@property (nonatomic, retain) NSMutableArray *bostonArray;

@end

最后是 LocationItems.m

- (void)viewDidLoad
{
[super viewDidLoad];
bostonArray = [[NSMutableArray alloc] init];
[self populateArray];


//********************************************************
//If string = boston load some array containing the cities.

// Do any additional setup after loading the view from its nib.
}

- (void) populateArray {

[bostonArray addObject:arrayToBePopulatedString];
//[bostonArray addObject:populationArray];
//[bostonArray addObject:@"Boston"];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

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

    [[cell textLabel] setNumberOfLines:1]; 
    //[[cell textLabel] setLineBreakMode:

     }

// Configure the cell.
[cell.textLabel setText:[bostonArray objectAtIndex:indexPath.row]];
return cell;
}

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [bostonArray.self count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"Row Selected %i",indexPath.row);




}

对于这么长的问题,我提前道歉,但相信有必要充分解释我的问题。我只是希望每个单元格都有一个不同的单词(第一、第二、第三等)。任何帮助将不胜感激。

谢谢!

4

3 回答 3

1

我不确定我是否理解您的意思:

我将所有字符串发送到一个组合字符串中,然后用它填充一个数组。

您的意思是您希望拆分包含所有子字符串的字符串并从中创建一个数组?

根据我从您的代码中得到的信息,您获取连接的字符串,其中所有值都用空格分隔,并将其存储为数组的第一个元素:

destination.arrayToBePopulatedString = [myStrings componentsJoinedByString:@" "];
...
[bostonArray addObject:arrayToBePopulatedString];

这只是在数组中添加完整的字符串。

如果要将连接的字符串拆分回数组,则可能应该使用类似

bostonArray = [[destination.arrayToBePopulatedString componentsSeparatedByString: @" "] retain];

然后,您不再需要在viewDidLoad.

于 2013-06-26T19:33:49.247 回答
0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [bostonArray.self count]; // typo here? [self.bostonArray count]
}
于 2013-06-26T19:36:08.227 回答
0

最简单的解决方法是更改bostonArray addObject​​为bostonArray addObjectsFromArray.

使用 addObject 会将整个数组添加到 bostonArray 的第一个元素。

于 2013-06-26T19:36:45.213 回答