4

我正在创建一个 iPad 应用程序。根 UITableview 在导航控制器中有一个右栏按钮项。当您点击按钮时,它会显示一个弹出控制器。弹出框是一个 UITableViewController。当您在弹出窗口中点击一个单元格时,我如何传递该单元格中的数据并将其插入到根 UITableview 的单元格中?我搜索了 Apple 文档,但找不到我需要的东西。谁能把我推向正确的方向?

根表.h

@interface Roottable : UITableViewController<PopoverDelegate>

弹出框.h

    @protocol AthleteSelectPopoverDelegate <NSObject>
    @required
    -(void)selectedObject:(Object *)newObject;
    @end


@property (nonatomic, weak) id<PopoverDelegate> delegate;


    @property (readwrite, nonatomic) Object *currentObject;

@end

弹出框.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];
    }
}
4

4 回答 4

1

我想到了。希望我能帮助别人。我会先解释代码,然后将其发布在下面。基本上,我将根表视图的数据源“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];
}
于 2013-08-26T23:54:52.627 回答
1

您将所选单元格中的数据添加到主表的数据源委托中。

然后该数据源应该告诉主表一个单元格已插入到索引路径中。

于 2013-08-24T06:36:26.373 回答
0

我认为您应该有一个名称与delegate弹出框控制器不同的属性,因为UITableViewController已经有一个协议的delegate属性UITableViewDelegate;也许masterTable或什么。

然后在selectedObject:根 UITableView 的实现中,您可以插入行或将其添加到数据数组和reload表中。

糟糕,我的错... @geraldWilliam 是对的, UITableViewController 没有委托属性...

您所拥有的似乎应该可以工作... selectedObject:方法是否在委托中被调用?如果是这样,你用这种方法做什么?如果将对象添加到根视图的数据集(数组或字典或数据库)中,在其表视图中插入一行(或重新加载数据),它应该可以工作。

这是一些对我有用的代码。它不是来自弹出窗口,而是来自推送视图,但没有理由应该有所作为:

- (ThingStatus) thingPicker: (ThingPickerTableViewController *) thingPicker didSelectThing: (Thing *) thing {
    NSLog( @"Entering %s", __func__ );
    // Dismiss the pushed view controller (for you, the popover)
    [self.navigationController popViewControllerAnimated: YES];

    NSArray *startingList = self.currentCellObjectList;
    [self.databaseManager addThing: thing];
    NSArray *endingList = self.databaseManager.thingsForTableView;

    // Figure out the differences adding made...
    DiffResult *changes = [startingList simpleDiffWithArray: endingList];
    NSLog( @"%d deletions, %d insertions", changes.deletionCount, changes.insertionCount );
    // I only handle insertions in this code... deletions would be similar
    __block NSUInteger objIdx = 0;
    NSMutableArray *changeableThingList = [startingList mutableCopy];
    [changes.insertionIndexes enumerateIndexesUsingBlock: ^( NSUInteger idx, BOOL *stop ) {
        NSLog( @" - insert %@ at %d", [[changes.insertionObjects objectAtIndex: objIdx] name], idx );
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow: idx inSection: 0];
        [changeableThingList insertObject: [changes.insertionObjects objectAtIndex: objIdx] atIndex: idx];
        self.currentCellObjectList = changeableThingList;
        [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationRight];
        ++objIdx;
    }];
    [self.databaseManager save];
    return [self.databaseManager: thingStatus];

}

于 2013-08-24T03:16:48.433 回答
-2

这里有一些很好的代码可以帮助你。

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     // Return the number of rows in the section.
    return self.item.count;
 }

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    //Get the row
    Sport *rowSport = self.sports[indexPath.row];

    cell.textLabel.text = rowItem.itemName;
    cell.detailTextLabel.text = rowItem.section;

    return cell;
}

我希望这能帮到您。

于 2013-08-24T03:08:04.627 回答