didSelectRowAtIndexPath Segue 返回垃圾
Posted
技术标签:
【中文标题】didSelectRowAtIndexPath Segue 返回垃圾【英文标题】:didSelectRowAtIndexPath Segue returning junk 【发布时间】:2014-11-11 04:18:15 【问题描述】:所以,我试图将我选择的单元格传递给以前的 VC,每次单击任何行时,它总是只发送字符串“Apples”。我不确定为什么。有任何想法吗? 注意:mySelectedCell 是在 ChoicesTableViewController 的 .h 文件中初始化的 NSString 我试图从
传递数据的 .m 文件@interface ChoicesTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *items;
@property (strong, nonatomic) NSMutableDictionary *alphabetizedItems;
@property (strong, nonatomic) NSArray *unsortedKeys;
@property (strong, nonatomic) NSArray *sortedKeys;
@end
@implementation ChoicesTableViewController
- (id)initWithStyle:(UITableViewStyle)style
self = [super initWithStyle:style];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.items = [[NSMutableArray alloc] init];
[self.items addObject:@"Apples"];
[self.items addObject:@"Bread"];
[self.items addObject:@"Butter"];
[self.items addObject:@"Cheese"];
[self.items addObject:@"Eggs"];
[self.items addObject:@"Grapes"];
[self.items addObject:@"Ice Cream"];
[self.items addObject:@"Milk"];
[self.items addObject:@"Oranges"];
[self.items addObject:@"Oreos"];
self.alphabetizedItems = [self alphabetizeItems:self.items];
//Segue if the item is tapped
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
self.mySelectedCell = [self.items objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"unwindSegueAction" sender:self];
//unwind segue from add choice
- (IBAction)unwindSegueToChoices:(UIStoryboardSegue *)segue
AddChoiceViewController *sourceVC = segue.sourceViewController;
NSString *myNewItem = sourceVC.myTextField.text;
//NSString *myFinalString = [[myNewItem substringToIndex:1] capitalizedString];
NSString *stringCapitalized = [myNewItem capitalizedString];
[self.items addObject:stringCapitalized];
self.alphabetizedItems = [self alphabetizeItems:self.items];
//[self.arrayNames addObjectsFromArray:@[[MyDataChoices itemWithNewName:stringCapitalized]]];
[self.tableView reloadData];
//titles for talble view
#pragma mark Helper Methods
- (NSMutableDictionary *)alphabetizeItems:(NSArray *)items
NSMutableDictionary *buffer = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [items count]; i++)
NSString *item = [items objectAtIndex:i];
NSString *firstLetter = [[item substringToIndex:1] uppercaseString];
if ([buffer objectForKey:firstLetter])
[(NSMutableArray *)[buffer objectForKey:firstLetter] addObject:item];
else
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:item, nil];
[buffer setObject:mutableArray forKey:firstLetter];
NSArray *keys = [buffer allKeys];
for (int j = 0; j < [keys count]; j++)
NSString *key = [keys objectAtIndex:j];
[(NSMutableArray *)[buffer objectForKey:key] sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:buffer];
return result;
#pragma mark title indexing
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
NSArray *keys = [[self.alphabetizedItems allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [keys objectAtIndex:section];
return key;
# pragma mark main table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView
NSArray *keys = [self.alphabetizedItems allKeys];
return [keys count];
-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
//return self.arrayNames.count;
self.unsortedKeys = [self.alphabetizedItems allKeys];
self.sortedKeys = [self.unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [self.sortedKeys objectAtIndex:section];
NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key];
return [itemsForSection count];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//MyDataChoices *currentRow = self.arrayNames[indexPath.row];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell2" forIndexPath:indexPath];
//cell.textLabel.text = currentRow.myNameChoices;
NSString *key = [self.sortedKeys objectAtIndex:[indexPath section]];
NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key];
NSString *item = [itemsForSection objectAtIndex:[indexPath row]];
[cell.textLabel setText:item];
return cell;
# pragma Mark delete slide button
//Delete Swipe Button
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// Return NO if you do not want the specified item to be editable.
return YES;
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
NSArray *unsortedKeys = [self.alphabetizedItems allKeys];
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key];
if (itemsForSection.count == 1)
// Delete the whole section
[self.alphabetizedItems removeObjectForKey:key];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
else
// Delete the row from the data source
//int index = indexPath.row;
[self.alphabetizedItems removeObjectForKey:indexPath];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
else if (editingStyle == UITableViewCellEditingStyleInsert)
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
我将数据传递给的 .m 文件
#import "ShoppingListTableViewTableViewController.h"
#import "myData.h"
#import "ChoicesTableViewController.h"
@interface ShoppingListTableViewTableViewController ()
@property (strong, nonatomic) NSMutableArray *itemData;
@end
@implementation ShoppingListTableViewTableViewController
- (id)initWithStyle:(UITableViewStyle)style
self = [super initWithStyle:style];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
self.itemData = [[NSMutableArray alloc] init];
[self.itemData addObjectsFromArray:@[
[myData itemWithName:@"test"]]];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// Return the number of sections.
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
return self.itemData.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
myData *currentRow = self.itemData[indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath];
cell.textLabel.text = currentRow.myName;
NSString *stringCount = [NSString stringWithFormat:@"%d", currentRow.count];
cell.detailTextLabel.text = stringCount;
cell.imageView.image = [UIImage imageNamed:@"Box-NoCheckMark.png"];
return cell;
//unwind segue data from choicestableviewcontroller
- (IBAction)unwindSegueAction:(UIStoryboardSegue *)segue
ChoicesTableViewController *vcb = (ChoicesTableViewController *)segue.sourceViewController;
BOOL isFound = false;
for (myData *item in self.itemData)
if( [item.myName isEqualToString:vcb.mySelectedCell])
item.count ++;
isFound = true;
break;
if(!isFound)
[self.itemData addObjectsFromArray:@[[myData itemWithName:vcb.mySelectedCell]]];
[self.tableView reloadData];
//Box checked
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
myData *check;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath];
if(check.checkMark)
cell.imageView.image = [UIImage imageNamed:@"Box-NoCheckMark.png"];
check.checkMark = NO;
else
cell.imageView.image = [UIImage imageNamed:@"Box-CheckMark.png"];
check.checkMark = YES;
//Delete Button when Swiped to the left
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// Return NO if you do not want the specified item to be editable.
return YES;
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
// Delete the row from the data source
int index = indexPath.row;
[self.itemData removeObjectAtIndex:index];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
else if (editingStyle == UITableViewCellEditingStyleInsert)
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
@end
【问题讨论】:
【参考方案1】:您需要使用与cellForRowAtIndexPath
indidSelectRowAtIndexPath
相同的逻辑来获取正确的字符串;你不能只使用 [self.items objectAtIndex:indexPath.row]。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSString *key = [self.sortedKeys objectAtIndex:[indexPath section]];
NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key];
self.mySelectedCell = [itemsForSection objectAtIndex:[indexPath row]];
[self performSegueWithIdentifier:@"unwindSegueAction" sender:self];
【讨论】:
这非常有效。我不知道我在想什么。非常感谢!以上是关于didSelectRowAtIndexPath Segue 返回垃圾的主要内容,如果未能解决你的问题,请参考以下文章
我的 didSelectRowAtIndexPath 没有被调用
didSelectRowAtIndexPath 没有被调用?
tableView - didSelectRowAtIndexPath 未调用