当我从PRIME类导入可变数组“primearray”并将其分配给视图控制器中声明的nsmutablearray“array”时,它会抛出错误。
基本上我试图用nsmutablearry的内容填充tableview。
在课堂上声明的这个mutablearry的内容取决于用户在操作表中按下index:0处的按钮。
任何人都可以帮忙吗?提前谢谢。
IBOutlet UITextField *txt1;
IBOutlet UITextField *txt2;
NSMutableArray *arry;
int startno;
int endno;
}
@property IBOutlet UITableView *tbl;
-(IBAction)ok:(id)sender;
@end
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
@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;
#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
答案 0 :(得分:0)
您遇到的主要问题是您想要使用PRIME类中的方法,但您根本没有初始化或使用
我认为不是
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];
}
}
我希望这会对你有所帮助。
答案 1 :(得分:0)
唯一丢失的帽子是阻止普通标签单元格中的素数输出; 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?????????
}