将UITableCellView中的值保存到自定义类

时间:2014-03-16 21:23:12

标签: ios objective-c uitableview save

我有一个iOS应用程序,它向Web服务发出请求,返回JSON格式的数据。我的iOS应用程序中有一个预定义的类,它继承并实现了JSONModel Framework,此返回的数据作为包含这些对象的NSMutableArray绑定到该框架。 TableView的数据是从这些对象生成的。

我的难题是,在我的自定义UITableViewCell中,我允许用户更改一些显示的数据,我需要能够将其保存回可以序列化并通过POST发送回Web服务的类。

自定义单元格.h:

@interface EnclosureDetailCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *enclosureNumber;
@property (weak, nonatomic) IBOutlet UITextField *QTY;
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
@property (weak, nonatomic) IBOutlet DeSelectableSegmentControl *enclosureStatus;

- (IBAction)valueChanged:(UIStepper *)sender;
- (IBAction)changedTextValue:(id)sender;

@end

Custom Cell .m:

@implementation EnclosureDetailCell

- (IBAction)changedTextValue:(id)sender
{
    self.stepper.value = self.QTY.text.intValue;
}

- (IBAction)valueChanged:(UIStepper *)sender
{
    int stepperValue = sender.value;
    self.QTY.text = [NSString stringWithFormat:@"%i", stepperValue];
}

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

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

    // Configure the view for the selected state
}

@end

模型类(.h):

@protocol Enclosure @end
@interface Enclosure : JSONModel

@property (nonatomic, strong) NSString *EnclosureNumber;
@property (nonatomic, strong) NSString *InventoryID;
@property (nonatomic, strong) NSString *UseInventoryID;
@property (nonatomic) int CensusQTY;
@property (nonatomic) BOOL Verified;
@property (nonatomic) BOOL MissingEnclosure;
@property (nonatomic) BOOL RetireEnclosure;
@end

TableViewController(部分)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    EnclosureDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    ProtocolEnclosure *loc = (ProtocolEnclosure *)_objects[indexPath.section];
    Enclosure *enc = (Enclosure *) loc.Enclosures[indexPath.row];
    cell.enclosureNumber.text = enc.EnclosureNumber;
    cell.QTY.text =[NSString stringWithFormat:@"%i", enc.CensusQTY];
    cell.stepper.value = enc.CensusQTY;
    if (enc.Verified)
    {
        cell.QTY.enabled = false;
        cell.stepper.enabled = false;
        cell.enclosureStatus.selectedSegmentIndex = Verified;
    }
    else if (enc.MissingEnclosure)
        cell.enclosureStatus.selectedSegmentIndex = MissingEnclosure;
    else if (enc.RetireEnclosure)
        cell.enclosureStatus.selectedSegmentIndex = RetireEnclosure;
    else
        cell.enclosureStatus.selectedSegmentIndex = None;

    return cell;
}

enum{
    Verified = 0,
    MissingEnclosure = 1,
    RetireEnclosure = 2,
    None = -1
};

所以在我的UITableViewCell中,我有一个对应于CensusQTY的文本字段和一个SegmentControl,他们的选择对应于Verified / MissingEnclosure / RetireEnclosure。

如何将用户通过UI更改的数据保存回模型类? 我显然无法遍历每个UITableView行 - 因为出列,我只会获得当前在屏幕上的行。

有关如何实现这一目标的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:0)

可能最简单的方法是让单元格拥有对您在单元格的IBAction方法中更新的Model对象的弱引用。

@interface EnclosureDetailCell : UITableViewCell

...
@property(nonatomic, weak) Enclosure *enclosure;
...

@end


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    Enclosure *enc = (Enclosure *) loc.Enclosures[indexPath.row];
    cell.enclosure = enc;
    ...
}


@implementation EnclosureDetailCell

- (IBAction)changedTextValue:(id)sender
{
    self.stepper.value = self.QTY.text.intValue;
    //you have access to self.enclosure, do what you want
}

- (IBAction)valueChanged:(UIStepper *)sender
{
    int stepperValue = sender.value;
    self.QTY.text = [NSString stringWithFormat:@"%i", stepperValue];
    //you have access to self.enclosure, do what you want
}

答案 1 :(得分:0)

可能有很多方法可以做到这一点,我想到的最简洁的方法是为自定义单元格创建一个委托。 (你可以在.h中声明)。您的单元格应添加索引属性以跟踪它所引用的Enclosure实例。

@class EnclosureDetailCell;

@protocol EnclosureCellDelegate
@required
- (void) qtyDidUpdate:(EnclosureDetailCell*)cell;
- (void) stepperDidUpdate:(EnclosureDetailCell*)cell;
@end

@interface EnclosureDetailCell : UITableViewCell

@property (nonatomic,assign) NSInteger index;
@property (nonatomic,weak) id<EnclosureCellDelegate> delegate;
....

在你的.m中你必须给你的代表打电话

@implementation EnclosureDetailCell

- (IBAction)changedTextValue:(id)sender
{
    self.stepper.value = self.QTY.text.intValue;
    [self.delegate qtyDidUpdate:self];        
}

- (IBAction)valueChanged:(UIStepper *)sender
{
    int stepperValue = sender.value;
    self.QTY.text = [NSString stringWithFormat:@"%i", stepperValue];
    [self.delegate stepperDidUpdate:self];
} 

您必须在TableViewController中实现这些方法。它看起来像是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    EnclosureDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.index = indexPath.row;
    cell.delegate = self;
    ProtocolEnclosure *loc = (ProtocolEnclosure *)_objects[indexPath.section];
    Enclosure *enc = (Enclosure *) loc.Enclosures[indexPath.row];
    ....
}

- (void) qtyDidUpdate:(EnclosureDetailCell*)cell{
     Enclosure *enc = (Enclosure *)loc.Enclosures[cell.index];
     //Here you can update directly the items of your array
} 

- (void) stepperDidUpdate:(EnclosureDetailCell*)cell{
    Enclosure *enc = (Enclosure *)loc.Enclosures[cell.index];
    //Here you can update directly the items of your array
}

这样您就可以更新整个阵列,并且可以随时将新数据发送到您的网络服务。