[英]Populating table from array only shows up on the first cell
我在下一个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);
}
对于如此长的问题,我事先表示歉意,但认为有必要充分解释我的问题。 我只是希望每个单元格都有一个不同的词(第一,第二,第三等)。 任何帮助将不胜感激。
谢谢!
我不确定我是否理解您的意思:
我将所有字符串发送到一个组合字符串中,然后用它填充一个数组。
您是说要拆分包含所有子字符串的字符串并从中组成一个数组吗?
从我从代码中得到的结果来看,您采用了连接字符串,其中所有值都用空格分隔,并将其存储为数组的第一个元素:
destination.arrayToBePopulatedString = [myStrings componentsJoinedByString:@" "];
...
[bostonArray addObject:arrayToBePopulatedString];
这只是将完整的字符串添加到数组中。
如果您想将连接的字符串拆分回一个数组,则可能应该使用类似
bostonArray = [[destination.arrayToBePopulatedString componentsSeparatedByString: @" "] retain];
然后,您不再需要在viewDidLoad
分配数组。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [bostonArray.self count]; // typo here? [self.bostonArray count]
}
最简单的解决方法是将bostonArray addObject
更改为bostonArray addObjectsFromArray
。
使用addObject会使整个数组添加到bostonArray的第一个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.