如何将单元格插入到UITableViewController中

4
我正在创建一个iPad应用程序。根UITableview在导航控制器中有一个右侧的bar按钮项。当您点击按钮时,它会显示一个弹出控制器。弹出窗口是一个UITableViewController。当您在弹出窗口中点击单元格时,如何将该单元格中的数据传递并插入到根UITableview的单元格中?我搜索了苹果文档,但找不到需要的内容。有人能指引我正确的方向吗?
Roottable.h
@interface Roottable : UITableViewController<PopoverDelegate>

Popover.h

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


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


    @property (readwrite, nonatomic) Object *currentObject;

@end

popover.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];
    }
}

你的当前代码会得到什么结果?这个语句运行了吗——[_delegate selectedObject:_currentObject];?你有记录吗?根表视图控制器如何将自己设置为Popover的代理? - rdelmar
4个回答

1
我想通了,希望能帮到别人。首先解释一下代码,然后将其发布如下。基本上,我将根表视图“ObjectSelect”的数据源设置为名为“currentObjectArray”的NSMutableArray。ObjectSelect也是ObjectSelectPopoverDelegate。基本上,当在弹出窗口中点击单元格时,它会将被点击的对象添加到“currentObjectArray”中并重新加载表视图。

ObjectSelect.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

ObjectSelect.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];
}

1
你将选定单元格中的数据添加到主表的数据源委托中。
然后,该数据源应告诉主表在索引路径处插入了一个单元格。

0

我认为你应该在你的弹出控制器中拥有一个名称不同于delegate的属性,因为UITableViewController已经有了一个delegate属性用于UITableViewDelegate协议;也许是masterTable或其他什么。

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

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

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

这是一些对我有效的代码。它不是来自弹出窗口而是来自推送视图,但这没有任何区别的原因:

- (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];

}


UITableView有一个委托属性。UITableViewController则没有。 - geraldWilliam

-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;
}

希望这能对你有所帮助。


我知道如何填充单元格,但我不知道如何在弹出窗口单元格被点击时填充单元格。 - Josue Espinosa
单元格中的数据来自哪里? - jake1k
从委托方法返回的是一个单一对象,即在弹出视图中被点击的对象。 - Josue Espinosa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接