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

时间:2012-04-09 08:41:25

标签: iphone xcode

我在段控件中有两个数组和三个按钮我希望当我在默认情况下加载屏幕时,它会在表格视图中获得结果数组项目然后单击明天按钮它会获得nextArray值并显示在表格中

  -(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) 

3 个答案:

答案 0 :(得分:0)

问题在于您每次都要分配appointmentsOfDay,因此请删除以下代码,

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

并将其添加到viewdidload。

然后,

if (segmentedControl.selectedSegmentIndex == 0) {

        [appointmentsOfDay addObject:resultArray];

    }

else if (segmentedControl.selectedSegmentIndex == 1) {

        [appointmentsOfDay addObject:nextArray];

    }

答案 1 :(得分:0)

以下代码正在实施并可能解决您的问题

在我按下按钮时实现两个数组,然后另一个数组填充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];
}

在cellForRowAtIndexPath方法中实现以下代码

- (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];
}

这可以帮助您解决问题

答案 2 :(得分:0)

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

@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;
}

点击日期按钮时,只需致电reloadData