Tableview单元格值到核心数据

时间:2012-01-20 10:21:52

标签: iphone objective-c ios uitableview

我在tableview中使用了自定义单元格以获取不同的值,并将值保存到我使用UILongPressGestureRecognizer的核心数据中,这样当用户长按单元格时,将打开一个对话框,为用户提供添加值的选项将该特定细胞纳入核心数据。

我的代码是:

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

    NSString * cellValue;
    if (tableView == listTable) 
    {
       cellValue = [listVehicles objectAtIndex:indexPath.row];
    } 
    else // handle search results table view
    { 
        cellValue = [filteredListItems objectAtIndex:indexPath.row];
    }

    static NSString *CellIdentifier = @"vlCell";

    VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        NSLog(@"Cell Created");

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];

        for (id currentObject in nibObjects) {
            if ([currentObject isKindOfClass:[VehicleListCell class]]) {
                cell = (VehicleListCell *)currentObject;
            }
        }

        UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
        pressRecongnizer.minimumPressDuration = 0.5f;
        [cell addGestureRecognizer:pressRecongnizer];
        [pressRecongnizer release];
    }

    cell.textLabel.font = [UIFont systemFontOfSize:10];

    [[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
    [[cell direction] setImage:[UIImage imageNamed:@"south.png"]];

    cell.licPlate.text = cellValue;

    return cell;
}

for longpressgesture:

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer {

    UITableViewCell *cell = (UITableViewCell *) [recognizer view];
    NSString *text = cell.textLabel.text;

    NSLog(@"cell value: %@", text);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;

    [alert addButtonWithTitle:@"Add to Favourites"];
    [alert addButtonWithTitle:@"Take to Map"];

    [alert show];
}


-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSString *title = [alert buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Add to Favourites"])
    {
        NSLog(@"Added to favourites.");
    }
    else if([title isEqualToString:@"Take to Map"])
    {
        NSLog(@"Go to MapView");
    }
}

现在的问题是,如果我点击任何一个单元格,我只能获得一个单元格的值。

如何通过按下它来获取每个单元格的值,然后将其保存到核心数据?

编辑:

我已经为自定义单元格创建了一个带有xib文件的视图控制器:

其.h文件为:

@interface VehicleListCell : UITableViewCell{

IBOutlet UILabel *licPlate;
IBOutlet UILabel *commDate;

IBOutlet UIImageView *ignition;
IBOutlet UIImageView *direction;

IBOutlet UITableViewCell *cell;
}

@property (nonatomic, strong) IBOutlet UILabel *licPlate;
@property (nonatomic, strong) IBOutlet UILabel *commDate;
@property (nonatomic, strong) IBOutlet UIImageView *ignition;
@property (nonatomic, strong) IBOutlet UIImageView *direction;

@end

1 个答案:

答案 0 :(得分:0)

我不确定你如何设置你的VehicleListCell,但它可能具有与普通UITableView不同的属性,所以改变你的

UITableViewCell *cell = (UITableViewCell *) [recognizer view];

in

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer

VehicleListCell* cell = (VehicleListCell *)[recognizer view];

修改

试试这段代码

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer {

    VehicleListCell* cell = (VehicleListCell *)[recognizer view];
    NSString *text = cell.licPlate.text;

    NSLog(@"cell value: %@", text);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;

    [alert addButtonWithTitle:@"Add to Favourites"];
    [alert addButtonWithTitle:@"Take to Map"];

    [alert show];
}