-1

我想在选择 tableviewcell 后传递数据的属性。请指导我。我已经设置了 detailviewcontroller。DataArray 是我在 detailviewcontroller 中的可变数组。peripheralManager 是我的 NSbject。Peripheral 是我的设备。

- (void)viewDidLoad
{
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
  peripheralManager=[[PeripheralManager alloc]init];
  self.MeBle.enabled=NO;
  device=[[NSMutableArray alloc]init];

}
- (void)peripheralManagerDidConnectPeripheral:(PeripheralManager *)peripheral
{
  NSLog(@"%@",peripheral.deviceName);
  [device addObject:[NSString stringWithFormat:@" %@ ",peripheral.deviceName]];    
   //   [device addObject:peripheral];
  [self.deviceTable reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *simpleTableIdentifier=@"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  if(cell==nil)
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
   cell.textLabel.text=[device objectAtIndex:indexPath.row];
   return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //DetailViewController *detail=[self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
   // [self.navigationController pushViewController:detail animated:YES];

   //[self performSegueWithIdentifier:@"TableDetails" sender:[device objectAtIndex:indexPath.row]];

}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{


    if ([segue.identifier isEqualToString:@"TableDetails"]) {

    DetailViewController *detail=segue.destinationViewController;
    detail.dataArray=device;


}
}
4

2 回答 2

1

在 prepareForSegue 你应该得到当前的选择索引:

NSIndexPath *indexPath = [self.deviceTable indexPathForCell:sender];

现在,您可以仅将此索引传递给目标控制器,也可以将当前数组中的特定对象传递给您,这取决于您。

detail.idx = [indexPath row];

编辑:你不需要实施

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

就在

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

改为:

detailViewObject.objCurrentDevice=[device objectAtIndex:[[self.deviceTable indexPathForCell:sender] row]];
于 2012-10-28T08:56:09.313 回答
1

在界面生成器中,查看 seague 运行时属性部分,您可以创建多个相同样式但标识符不同的单元格,然后 cellForRowAtIndexPath 按行返回单元格

于 2012-10-28T17:13:45.233 回答