2013-06-26 42 views
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 

最后

- (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); 




} 

我提前道歉了这么长的问题,但认为有必要充分说明我的问题LocationItems.m。我只想让每个单元格有不同的词(第一,第二,第三等)。任何帮助将不胜感激。

谢谢!

+0

你的arrayToBePopulatedString看起来像什么?在LocationItems.m中使用ViewDidLoad中的NSLog – davis

+0

在populateArray方法下使用。 [bostonArray addObject:arrayToBePopulatedString]; – Tanner

回答

1

我不知道,我明白你的意思是:

,我将所有的字符串一个组合的字符串,然后填入一 阵列它。

你的意思是说你想拆分包含所有子字符串的字符串,并且创建一个数组吗?

从我从你的代码得到的,你把连接字符串,其中所有的值都用空格分开,你把它存储为一个数组的第一个元素:

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

这只是增加了完整的串在数组中。

如果你想在连接字符串分割回一个数组,你应该使用类似

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

然后,你不再需要在你的viewDidLoad分配数组。

+0

你好,这仍然留下第一个单元格中的所有单词。 – Tanner

+0

你好,对以前的评论感到抱歉。我的代码位于错误的位置。这工作。非常感谢! – Tanner

+0

很高兴帮助。我只注意到我缺少一个保留,因为componentsSeparatedByString返回一个自动释放对象。一旦自动发布,bostonArray就会成为一个不好的参考,当你尝试访问它时会导致随机崩溃。我已经更新了解决此问题的答案。 –

0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [bostonArray.self count]; // typo here? [self.bostonArray count] 
} 
+0

感谢您的支持。 – Tanner

0

最简单的方法是将bostonArray addObject更改为bostonArray addObjectsFromArray

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

+0

在他的情况下,arrayToBePopulatedString是一个NSString。他将原始数组转换为单个NSString,因此它将整个字符串添加为空数组的第一个元素。 –

+0

当使用arrayToBePopulatedString做这件事时,由于字符串不是数组,所以会发生崩溃。我应该解释为什么我会经历所有这些麻烦来填充表格。由于将会有多个城市,而不是有一个静态的,预先定义的视图,我希望能够根据用户选择的城市填充一个动态表格。 – Tanner

相关问题