0

次のセグエでテーブルビューを設定する際に問題が発生しています。基本的に、すべての文字列を 1 つの結合文字列で送信し、それを配列に入力します。その配列から、テーブルにデータを入力しています。問題は、これが最初のセルにのみ表示されることです。コンマを含む各エントリを次のセルに分割したいと思います。最初の 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);




}

このような長い質問を前もってお詫びしますが、私の問題を完全に説明する必要があると信じています。各セルに異なる単語(最初、2番目、3番目など)を持たせたいだけです。どんな助けでも大歓迎です。

ありがとう!

4

3 に答える 3

1

私はあなたが何を意味するのか理解できません:

すべての文字列を 1 つの結合文字列で送信し、それを配列に入力しています。

すべての部分文字列を含む文字列を分割し、それから配列を作成したいということですか?

あなたのコードから得たものから、すべての値がスペースで区切られている連結文字列を取得し、それを配列の最初の要素として保存します。

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

これは、完全な文字列を配列に追加するだけです。

連結された文字列を分割して配列に戻したい場合は、おそらく次のようなものを使用する必要があります

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

そうすれば、配列に配列を割り当てる必要がなくなりますviewDidLoad

于 2013-06-26T19:33:49.247 に答える