Iphone Tableview核心数据的单元格值
我在tableview中使用了自定义单元格来显示不同的值,为了将值保存到核心数据中,我使用了UILongPressGestureRecognitor,这样当用户长时间按下单元格时,就会打开一个对话框,让用户可以选择将特定单元格的值添加到核心数据中 我的代码是:Iphone Tableview核心数据的单元格值,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我在tableview中使用了自定义单元格来显示不同的值,为了将值保存到核心数据中,我使用了UILongPressGestureRecognitor,这样当用户长时间按下单元格时,就会打开一个对话框,让用户可以选择将特定单元格的值添加到核心数据中 我的代码是: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *
-(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;
}
长按手势:
- (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
我不确定如何设置VehicleListCell,但它的属性可能与普通UITableView不同,因此请更改您的设置
UITableViewCell *cell = (UITableViewCell *) [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];
}
我已经尝试了你的建议,并且成功了:)但是我还有一个问题与此相关。。。我的对话框不会在单击一次后消失,我必须单击(任一按钮)3次,然后对话框消失。。这背后可能有什么问题?您的NSLog是否打印了多次?(这一个NSLog(@“cell value:%@”,text);)如果这一个打印多次,可能是因为您的手势识别器检测到多个输入,但每次我长按时,需要单击对话框3次才能消失阅读此链接,这应该会有所帮助!干杯
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];
}