Iphone 如何在UISegment按钮上更改表视图的值单击

Iphone 如何在UISegment按钮上更改表视图的值单击,iphone,xcode,Iphone,Xcode,我在段控制中有两个数组和三个按钮,我希望在默认情况下加载屏幕时,它在表视图中获取结果数组项,然后单击“明天”按钮,它获取下一个数组值并显示在表中 -(void)viewDidLoad{ if(!resultArray){ resultArray =[[NSMutableArray alloc] init]; } [self setUpData]; if(!nextArray){ nextArray =[[NSMutableArray alloc] i

我在段控制中有两个数组和三个按钮,我希望在默认情况下加载屏幕时,它在表视图中获取结果数组项,然后单击“明天”按钮,它获取下一个数组值并显示在表中

  -(void)viewDidLoad{ 
  if(!resultArray){

    resultArray =[[NSMutableArray alloc] init];
}

   [self setUpData];

   if(!nextArray){

    nextArray =[[NSMutableArray alloc] init];
 }

    [self nextData];
   appointmentsOfDay = [[NSMutableArray alloc] arrayWithArray:resultArray copyItems:YES];
    }


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

 return [appointmentsOfDay count];



}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel * dobLabel = nil;
static NSString * Identifier = @"Identifier";
UITableViewCell * cell = [table dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier] autorelease];

    dobLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 6, 100, 32)];
    dobLabel.font = [UIFont systemFontOfSize:15];
    dobLabel.tag = 999;
    [cell addSubview:dobLabel];
    [dobLabel release];
 }
 else {
     dobLabel = (UILabel *)[cell viewWithTag:999];
 }

appDelegate = (EMRAppDelegate *)[[UIApplication sharedApplication] delegate];

 ObjectData *theCellData = [appointmentOfDay objectAtIndex:indexPath.row];





     cell.textLabel.font = [UIFont systemFontOfSize:15];
     cell.textLabel.textAlignment = UITextAlignmentLeft;
     cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",theCellData.firstName,theCellData.lasttName];

    dobLabel.text = theCellData.appointmentTime;





      return cell;
    }


  - (void)onDayButtonClick:(id)sender
  {
     UISegmentedControl * segmentedControl = (UISegmentedControl *)sender;


   [appointmentsOfDay removeAllObjects];
    if (segmentedControl.selectedSegmentIndex == 0) {


        [appointmentsOfDay removeAllObjects];


         appointmentsOfDay = [[NSMutableArray alloc] arrayWithArray:resultArray copyItems:YES];



    }

    else if (segmentedControl.selectedSegmentIndex == 1) {

        [appointmentsOfDay addObject:nextArray];


    }
    else if (segmentedControl.selectedSegmentIndex == 2) {

    }

[table reloadData];
}
事故报告

 EMR[1161:10703] -[__NSPlaceholderArray arrayWithArray:copyItems:]: unrecognized selector sent to instance 0x7320120
2012-04-09 14:13:35.021 EMR[1161:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderArray arrayWithArray:copyItems:]: unrecognized selector sent to instance 0x7320120'
 *** First throw call stack:
 (0x201d052 0x2d01d0a 0x201eced 0x1f83f00 0x1f83ce2 0xc7a7 0x6bc64e 0x6bc941 0x6ce47d  0x6ce66f 0x6ce93b 0x6cf3df 0x6cf986 0x6cf5a4 0x9660 0x201eec9 0x5f95c2 0x5f955a 0x69eb76 0x69f03f 0x69e2fe 0x61ea30 0x61ec56 0x605384 0x5f8aa9 0x2f72fa9 0x1ff11c5 0x1f56022 0x1f5490a 0x1f53db4 0x1f53ccb 0x2f71879 0x2f7193e 0x5f6a9b 0x25bd 0x2535)
terminate called throwing an exception(lldb) 

问题是您每天都在分配任命,因此请删除以下代码

 appointmentsOfDay = [[NSMutableArray alloc] arrayWithArray:resultArray copyItems:YES];
并将其添加到viewdidload

那么


下面的代码正在实现,可能会解决您的问题

在本例中,我在按下按钮时实现两个数组,然后另一个数组填充Uitable

在视图中,load方法是否实现了以下代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.arOne=[NSArray arrayWithObjects:@"1",@"a",@"b",@"c",@"d",@"e", nil];
    self.arTwo=[NSArray arrayWithObjects:@"x",@"y",@"z",@"o",@"p", nil];
    [self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text=[(self.btnTapped.selected)?self.arTwo:self.arOne objectAtIndex:indexPath.row];

    return cell;
}
在cellForRowAtIndexPath方法中实现以下代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.arOne=[NSArray arrayWithObjects:@"1",@"a",@"b",@"c",@"d",@"e", nil];
    self.arTwo=[NSArray arrayWithObjects:@"x",@"y",@"z",@"o",@"p", nil];
    [self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text=[(self.btnTapped.selected)?self.arTwo:self.arOne objectAtIndex:indexPath.row];

    return cell;
}
控制和方法之间连接的IBAction方法

- (IBAction)btnTitle:(id)sender {
    self.btnTapped.selected=!self.btnTapped.selected;
    [self.tableView reloadData];
}

希望这有助于解决您的问题

这是我更喜欢使用的方法。请参考分段控件。声明只读属性

@property (nonatomic, readonly) NSArray *appointmentOfDay;
并作为

-(NSArray*) appointmentOfDay {
    NSArray *tData;
    if (self.segmentedControl.value==0) {
        //instantiate tData based on value
    }
    // do for all segmented control value
    return tData;
}

单击Day按钮时只需调用reloadData。

问题是如何在按钮单击时更改表数据我有两个数组中的数据NSMutableArrays您已经为其编写了代码吗?是的,但我不会像这样完成此操作。这里是访问教程的参考url,用于将多个数组填充到它正在崩溃的UITableview中arrayWithArray:resultArray copyItems:YES在viewDidLoad AppointsofDay=[[NSMutableArray alloc]arrayWithArray:resultArray copyItems:YES];这是我写的!resultArray{resultArray=[[NSMutableArray alloc]init];}[self-setUpData];如果nextArray{nextArray=[[NSMutableArray alloc]init];}[self-nextData];AppointsofDay=[[NSMutableArray alloc]arrayWithArray:resultArray copyItems:YES];检查是否在viewDidLoad方法中。您可以提供崩溃报告吗?