简体   繁体   English

从UItableviewcell重新加载TableView数据

[英]Reload TableView data from UItableviewcell

I have an IBaction in my subclass of UITableviewcell, and I am having difficulties to make it reload the data of the tableview. 我的UITableviewcell子类中有一个IBaction,并且很难使其重新加载tableview的数据。 It looks simple, but I tried lot of staff and nothing was working. 看起来很简单,但我尝试了很多工作人员,但没有任何效果。

Following, the code I am using in my UITableviewcell, but nothing reload: 以下是我在UITableviewcell中使用的代码,但是没有重载:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (IBAction)deleterow:(id)sender
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Delete content ?"
                                                      message:@"Do you want to delete ?"
                                                     delegate:self
                                            cancelButtonTitle:@"Cancelar"
                                            otherButtonTitles:@"Eliminar", nil];
    [message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:@"Eliminar"])
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        
        NSString *documentsDirectory = [paths objectAtIndex:0];        
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"datatesting.plist"];
        libraryContent = [[NSMutableArray alloc] initWithContentsOfFile:path];
        [libraryContent removeObjectAtIndex:0];
        NSLog(@"delete the Row:%@",libraryContent);
        [libraryContent writeToFile:path atomically:YES];

        UITableView *parentTable = (UITableView *)self.superview;
        [parentTable reloadData];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
 }

 @end

What is the best way to reload data from an UITableviewcell ? 从UITableviewcell重新加载数据的最佳方法是什么?

I'm not completely sure how your code is connected, but typically for things like this, you should use a delegate pattern by either passing a reference of your tableview into a property of your subclass: 我不完全确定代码的连接方式,但是通常对于此类事情,应该通过将表视图的引用传递到子类的属性中来使用委托模式:

subclass *s = [subclass alloc] init];
s.table = self.tableview;

then when you want to reference the parent table do so this way: 然后,当您想引用父表时,可以这样:

[self.table reloadData];

or by using a protocol/delegate combo which would really be the preferred approach. 或使用协议/代理组合,这确实是首选方法。 Like this: 像这样:

in subclass: 在子类中:

@protocol subclassdelegate <NSObject>
- (void)refreshParentTableView;
@end

set delegate in interface 在接口中设置委托

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

then call it when you need it like this 然后在需要时调用它

[self.delegate refreshParentTableView];

now, in the parent you need to do a couple of things call your subclass and set self as the delegate property 现在,在父级中,您需要做一些事情来调用您的子类,并将self设置为委托属性

subclass *s = [subclass alloc] init];
s.delegate = self;

then in the parent class implement the method defined in the protocol 然后在父类中实现协议中定义的方法

- (void)refreshParentTableView {
     [self.tableview reloadData]
}

both approaches work, but I'd advise you to use the protocol approach. 两种方法都可以,但是我建议您使用协议方法。 It's mode code, but much easier to understand and probably more reliable. 它是模式代码,但是更容易理解,并且可能更可靠。

be well. 很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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