我在下一个 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);
}
对于这么长的问题,我提前道歉,但相信有必要充分解释我的问题。我只是希望每个单元格都有一个不同的单词(第一、第二、第三等)。任何帮助将不胜感激。
谢谢!