-1

ここにテキストフィールドを含むアラートビューのコードを実装しました。[OK]ボタンをクリックすると、入力したテキストフィールドの値が文字列形式で(str)として格納され、配列をテーブルビューにロードするために、その値を配列に追加しますが、ここでの問題は、テーブルビューが1つのアイテムで構成されるたびですが、アラートからアイテムを入力する複数のアイテムを保存し、それを配列アイテムとして保存したいのですが、アラートビューを介して複数の配列アイテムを保存するためのコードを実装する方法は、iphoneでこの問題を解決するのに役立ちます。

myAlert = [[UIAlertView alloc] initWithTitle:@"Enter year" 
                                     message:@"alert message" 
                                    delegate:self 
                           cancelButtonTitle:@"Cancel" 
                           otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:@"" label:@"entervalue"];     
alertTextField=[myAlert textFieldAtIndex:0];
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert show];
[myAlert release];


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"Ok"]) {

        str = alertTextField.text;

        [savedarray addObject:str];
    }
}
4

3 に答える 3

2
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
  if ([buttonTitle isEqualToString:@"Ok"]) {

    str = alertTextField.text;

    [savedarray addObject:str];
    [self.yourtableview reloadData]; // reload your tableview when add new data in array.
  }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section        {
return savedarray.count;
 }

- (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.text = [savedarray objectAtIndex:indexPath.row];    
return cell;
}

うまくいけば、これはあなたを助けるでしょう..

于 2012-05-03T05:01:14.733 に答える
0

私がこのようなものを求めている場合:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
 NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
 if ([buttonTitle isEqualToString:@"Ok"]) 
 {
     str = alertTextField.text;
     if([savedarray Count] < yourRequiredItemsCount)
     {
      [savedarray addObject:str];
      alertTextField = @"";
      [alertView show];
     }
     else
     {
      [alertView release];                //Release the Alert here.
     }
 }
}

それが役立つかどうか教えてください!あなたに代替ソルを与えるでしょう。

于 2012-05-03T04:45:28.560 に答える
0

savedarrayを正しく割り当てている場合、投稿されたコードは妥当なように見えます。次に確認するのは、テーブルのデータソースとしてsavedarrayを適切に使用しているかどうかです。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return savedarray.count;
}

- (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.text = [savedarray objectAtIndex:indexPath.row];    
    return cell;
}

テーブルビューのデリゲートおよびデータソースと同じビューコントローラを設定してください。

于 2012-05-03T04:50:42.920 に答える