我想到了。希望我能帮助别人。我会先解释代码,然后将其发布在下面。基本上,我将根表视图的数据源“ObjectSelect”设置为NSMutableArray
称为“currentObjectArray”。ObjectSelect 也是 ObjectSelectPopoverDelegate。基本上,当点击弹出窗口中的单元格时,它会将点击的对象添加到“currentObjectArray”并重新加载表格视图。
对象选择.h
#import <UIKit/UIKit.h>
#import "ObjectSelectPopover.h"
@interface ObjectSelect : UITableViewController<ObjectSelectPopoverDelegate>
@property (nonatomic, strong) ObjectSelectPopover *objectPicker;
@property (nonatomic, strong) UIPopoverController *objectPickerPopover;
@property (readwrite, nonatomic) Object *currentObject;
@property (nonatomic, strong) NSMutableArray *selectedObjectArray;
@end
对象选择.m
-(void)selectedObject:(Object *)newObject
{
_currentObject = newObject;
if(!_selectedObjectArray){
_selectedObjectArray = [[NSMutableArray alloc] init];
}
if([_selectedObjectArray containsObject:_currentAthlete]){
//lol you don't get added, bub
}
else{
[_selectedObjectArray addObject:_currentObject];
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Object *objectTapped = (Object *)[_objectAthleteArray objectAtIndex:indexPath.row];
return cell;
}
ObjectSelectPopover.h
#import <UIKit/UIKit.h>
#import "Object.h"
@protocol ObjectSelectPopoverDelegate <NSObject>
@required
-(void)selectedObject:(Object *)newObject;
@end
@interface ObjectSelectPopover : UITableViewController
@property (nonatomic, weak) id<ObjectSelectPopoverDelegate> delegate;
@property (nonatomic, strong) NSMutableArray *objectArray;
@property (readwrite, nonatomic) Object *currentObject;
@end
ObjectSelectPopover.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_currentObject = [_objectArray objectAtIndex:indexPath.row];
//Notify the delegate if it exists.
if (_delegate != nil) {
[_delegate selectedObject:_currentObject];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}