单击操作时填充 TableView sheet Objective-C Prime 否,从文本字段输入的值

Populating TableView upon clicking action sheet Objective-C Prime No, value entered from textfield

当我从 PRIME class 导入可变数组 "primearray" 并将其分配给在视图控制器中声明的 nsmutablearray "array" 时,它会抛出错误。

基本上我试图用 nsmutablearry 的内容填充 tableview。

在 class 中声明的这个 mutablearry 的内容取决于用户在 index:0 处按下按钮 sheet。

有人能帮忙吗???提前致谢。

 IBOutlet UITextField *txt1;

    IBOutlet UITextField *txt2;

    NSMutableArray *arry;

    int startno;

    int endno;    
}

@property IBOutlet UITableView *tbl;

-(IBAction)ok:(id)sender;


@end

VIEWCONTROLLER.H 文件结束

 ViewController.m


    #import "ViewController.h"

    #import "PRIME.h"

    #import "NON-PRIME.h"

    #import "ODD.h"

    #import "EVEN.h"

   @interface ViewController ()

   @end

    @implementation ViewController

    - (void)viewDidLoad
    {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a  nib.


   arry=[[NSMutableArray alloc]init];



}

      - (BOOL)textFieldShouldReturn:(UITextField *)textField
      {
          [textField resignFirstResponder]; 


          return YES;
      }

        -(IBAction)ok:(id)sender
  {
       // arry=[[NSMutableArray alloc]init];


          startno=[txt1.text intValue];

         endno=[txt2.text intValue];

       if(startno ==0 || endno ==0 || startno > endno)
       {

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"CHECK   INPUT "message:@"input must not be nil,zero or alphabet & endno must be  greater than startno" delegate:self cancelButtonTitle:@"CANCEL"  otherButtonTitles:nil, nil];

    [alert show];

      }

 else

  {


  //if (startno !=0 || endno !=0 || !(startno > endno))
    {
      NSLog(@"1....start @%d, end @%d",startno,endno);

      UIActionSheet *action=[[UIActionSheet   alloc]initWithTitle:@"select your choice" delegate:self  cancelButtonTitle:@"cancel:" destructiveButtonTitle:nil otherButtonTitles:@"prime",@"non-prime",@"odd",@"even", nil];

       [action showInView:self.view];
    }
}

}

    - (void)actionSheet:(UIActionSheet *)actionSheet   clickedButtonAtIndex: (NSInteger)buttonIndex
    {

    if(buttonIndex==0)
{

    NSLog(@"2...start @%d, end @%d",startno,endno);

   PRIME *prime=[[PRIME alloc]init];

      [prime setnumbers1:startno sec:endno];

    [arry addObjectsFromArray:[prime primecal]];


    for(int i=0;i<arry.count;i++)
    {
        NSLog(@"count : @%d",i);

    }
     for(count1 in arry)
     {

        NSLog(@" arry %@ ",count1);

    }
 }


if(buttonIndex==1)
{
    NON_PRIME *nonprimeobj=[[NON_PRIME alloc]init];
}
if(buttonIndex==2)
{
      ODD *oddobj=[[ODD alloc]init];

}
if(buttonIndex==3)
{

    EVEN *evenobj=[[EVEN alloc]init];

}

}

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


      - (UITableViewCell *)tableView:(UITableView *)
        tableView      cellForRowAtIndexPath:(NSIndexPath *)indexPath
          {

            UITableViewCell *cell;


        if(cell==nil)
           {

            cell=[[UITableViewCell    alloc]initWithStyle:
            UITableViewCellStyleSubtitle reuseIdentifier:@"A"];

           }
       cell.textLabel.text=[arry objectAtIndex:indexPath.row];

       return cell;



  }

   - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

   }  

   @end

视图结束controller.m 文件

 @interface PRIME : NSObject
    {

       int i,start,end;

       int remain,k,p;

       int count;

    //int count=0;   //error????

   }

    @property(strong,nonatomic) NSMutableArray *primearray;

   -  (void)setnumbers1:(int)start1 sec:(int)end1;

    -(NSMutableArray *)primecal;

PRIME.H 结束 CLASS

     #import "PRIME.h"

      @implementation PRIME


     -(void)setnumbers1:(int)start1 sec:(int)end1
      {

       start=start1;

      end=end1;

  }



     -(NSMutableArray *)primecal
     {
        NSMutableArray *primearray=[[NSMutableArray alloc]init];

          for(i=start;i<=end;i++)
         {

           for(p=2;p<=1;p++)

        {
            remain=i%p;

            p++;
        }


        if(p==i)

        {
             [primearray addObject:[NSString stringWithFormat:@"%d",i]];

            count++;
        }
    }

    return  primearray;
   }

@end

PRIME.M 结束 CLASS

您遇到的主要问题是您想使用 PRIME 中的方法 class 但您根本没有初始化或使用

我认为

arry=[[NSMutableArray alloc]init];

startno=[txt1.text intValue];

endno=[txt2.text intValue];

arry=[arry addObject: primearray]; // 

你需要这样的东西

NSMutableArray * arry = [NSMutableArray array];
startno=[txt1.text intValue];
endno=[txt2.text intValue];

PRIME * prime = [[PRIME alloc]init];
[prime setnumbers1:startno sec:endno];
[arry addObjectsFromArray:[prime primeCal]];

这是在使用您的 actionSheet

- (void)actionSheet:(UIActionSheet *)actionSheet   clickedButtonAtIndex: (NSInteger)buttonIndex
{

    if(buttonIndex==0)
    {
        startno=[txt1.text intValue];
        endno=[txt2.text intValue];

        PRIME * prime = [[PRIME alloc]init];
        [prime setnumbers1:startno sec:endno];
        [arry addObjectsFromArray:[prime primeCal]];
        [self.tableView reloadData]
    }

    if(buttonIndex==1)
    {
        NON_PRIME *nonprimeobj=[[NON_PRIME alloc]init];
    }
    if(buttonIndex==2)
    {
          ODD *oddobj=[[ODD alloc]init];

    }
    if(buttonIndex==3)
    {

        EVEN *evenobj=[[EVEN alloc]init];

    }
}

希望对您有所帮助。

唯一缺少的帽子是防止在普通选项卡的单元格中输出素数;e 视图如下代码添加:

- (void)actionSheet:(UIActionSheet *)actionSheet 
 clickedButtonAtIndex:  (NSInteger)buttonIndex
{

 [self.tbl reloadData];  //ADDED


}




- (NSInteger)tableView:(UITableView *)tableView                    numberOfRowsInSection:(NSInteger)section
{

  return arry.count;

  NSLog(@"arry.count @%d",arry.count);  /// //STILL not working????????? 
 }