简体   繁体   中英

Get indexPath.row from tableView into cell

EDIT: Question in short: How do I access the index of the cell from the UICell class? Because atm I get it only from the method in the UITableView class which is posted beneath. end of edit

I have an iOS app and added a UITableView. I'm filling it with custom UICells and in the UITableView I use this method to delete a row and remove the objects from the datasource

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

I also added a modify-button in the UICell, so I also have to access the datasource, but I can't because it is is the UITableView class and I can't access it from the UICell class.

What is the best solution for this? Do I have to create a class for my datasource and both classes (UITableView and UICell) access it? If so, how do I do that? Sorry I am new to MVC, please help :)

Thanks in advance

I think that the best way is to implement the Pattern Singleton

Regards.

#import "MySingleton.h"

@implementation MySingleton
+(MySingleton *) sharedInstance{
   static  MySingleton *inst = nil;

    @synchronized(self){
        if (!inst) {
            inst = [[self alloc] init];
        }
    }
   return inst;
}

@end

When I've needed to do things like that, my approach has been to use a NSNotification . The cell fires a custom notification and attaches self as the notification object. The data source listens for the notification and either handles the action itself or responds by extracting the cell object and sending whatever it needs back to it.

您可能正在从数组中填充表,如果是这种情况,则可以在index(selected index)处删除数组元素并重新加载tableview。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM