我有一个UITableview,在那个用json数据填充的TableView I中。我已经实现了一个多选的tableview代码,用一个复选标记检查触摸的表格单元格。
我的问题是如何辨别我选择的哪个tableview单元格以及如何将动作放到选定的表格视图单元格中?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.json2.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = self.json2[indexPath.row][@"SurveyName"];
//cell.detailTextLabel.text = self.json1[indexPath.row][@"AssetDesc"];
// Sets the accessory for the cell
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}
通常如果我要从tableview单元格中创建一个segue,我会执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform segue
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier: @"showSurveyForm" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showSurveyForm"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CameraViewController *destViewController = segue.destinationViewController;
// Hide bottom tab bar in the detail view
destViewController.hidesBottomBarWhenPushed = YES;
destViewController.surveyQuestionId = self.surveyQuestionIDParsed;
destViewController.jsonTitleforSurvey = self.json2;
destViewController.surveyTitleAssetId = self.assetSurvey;
NSLog(@"Survey ID for Survey: %@", self.surveyQuestionIDParsed);
NSLog(@"Survey name titile: %@", self.json2 );
NSLog(@"Asset ID for Title: %@", self.assetSurvey);
}
else if ([segue.identifier isEqualToString:@"showCameraRoll"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// Hide bottom tab bar in the detail view
}
else if ([segue.identifier isEqualToString:@"showJsonData"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
}
else if ([segue.identifier isEqualToString:@"testPickerDemo"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
}
}
我想在UItableview中选择多个选定的单元格后制作一个segue。
例如,我进行选择并检查2行。我想在我的nslog中捕获或查看哪个tableview单元格被选中,这样我就可以根据所选行执行另一个json调用和segue。
我知道如何调用和解析json并制作一个segue。
答案 0 :(得分:3)
您可以修改-didSelectRowAtIndexPath
这样的内容:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selStr = [yourSourceArray objectAtIndex:indexPath.row];
---> NSMutableArray *selArray = [[NSMutableArray alloc] init]; //Write this line in viewDidLoad method.
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
[selArray addObject:selStr];
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
[selArray removeObject:selStr];
}
NSLog(@"selArray :: %@",selArray);
}
更新:
它将为您提供 textLabel的Selected Rows 文本数组。然后,您可以在-yourButtonPressed
方法
- (IBAction)yourButtonPressed:(id)sender;
{
NSLog(@"selArray :: %@",selArray);
}
GoodLuck !!!