Ios 在表视图上选择后更改按钮状态
我有一个应用程序,用户可以喜欢和不喜欢我的表视图中的不同项目。每个表视图单元都有一个like按钮,单击该按钮时执行like操作,该操作通过增加我保存的字典中的like计数来记录。然后,用户可以再次单击按钮,与行不同,从而减少like计数。单击like按钮时,将调用以下方法:Ios 在表视图上选择后更改按钮状态,ios,uitableview,ios7,uibutton,Ios,Uitableview,Ios7,Uibutton,我有一个应用程序,用户可以喜欢和不喜欢我的表视图中的不同项目。每个表视图单元都有一个like按钮,单击该按钮时执行like操作,该操作通过增加我保存的字典中的like计数来记录。然后,用户可以再次单击按钮,与行不同,从而减少like计数。单击like按钮时,将调用以下方法: -(void)next:(ui按钮*)按钮 现在我正在使用[button setAlpha:0.1](更改不透明度)以指示是否喜欢它这是我的问题:由于某种原因,另一个表视图单元格受到影响/更改,而不是目标单元格!例如,如果我
-(void)next:(ui按钮*)按钮
现在我正在使用[button setAlpha:0.1]代码>(更改不透明度)以指示是否喜欢它这是我的问题:由于某种原因,另一个表视图单元格受到影响/更改,而不是目标单元格!例如,如果我调用这个方法并单击第一行,那么第四行也会受到影响
喜欢与不喜欢在这里被称为:
- (void)likeToObject:(BOOL)liked object:(PFObject*)object {
PFQuery *query = [PFQuery queryWithClassName:@"Item"];
[query whereKey:@"objectId" equalTo:[object objectId]];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *foundObject, NSError *error) {
if (!error) {
if (liked) {
int currentLikes = [foundObject[@"likes"] intValue];
[foundObject setObject:[NSNumber numberWithInt:currentLikes+1] forKey:@"likes"];
[foundObject saveInBackground];
[self loadObjects];
[self.tableView reloadData];
} else {
int currentLikes = [foundObject[@"likes"] intValue];
[foundObject setObject:[NSNumber numberWithInt:currentLikes-1] forKey:@"likes"];
[foundObject saveInBackground];
[self loadObjects];
[self.tableView reloadData];
}
} else {
[self.tableView reloadData];
}
}];
}
谢谢你的帮助
编辑:这是我的UI/表格视图代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"ParseProduct";
PFProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell) {
cell = [[PFProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
PFObject *product = self.objects[indexPath.row];
[cell configureProduct:product];
[cell.orderButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
cell.orderButton.tag = indexPath.row;
[cell.buyButton addTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
cell.buyButton.tag = indexPath.row;
[cell.shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
cell.shareButton.tag = indexPath.row;
return cell;
}
- (void)next:(UIButton *)button {
PFObject *object = [self.objects objectAtIndex:button.tag];
if (![[likesDict objectForKey:object.objectId] boolValue]) {
[likesDict setObject:@YES forKey:object.objectId];
[self likeToObject:YES object:object];
} else {
[likesDict setObject:@NO forKey:object.objectId];
[self likeToObject:NO object:object];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[likesDict writeToFile:[documentsDirectory stringByAppendingPathComponent:@"likes.plist"] atomically:YES];
[button setAlpha:0.1];
}
- (void)configureProduct:(PFObject *)product {
UIImage *backgroundImage = [UIImage imageNamed:@"Screen Shot 2014-06-16 at 6.34.53 PM.png"];
UIEdgeInsets backgroundInsets = UIEdgeInsetsMake(backgroundImage.size.height/2.0f, backgroundImage.size.width/2.0f, backgroundImage.size.height/2.0f, backgroundImage.size.width/2.0f);
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[backgroundImage resizableImageWithCapInsets:backgroundInsets]];
self.backgroundView = backgroundImageView;
self.imageView.file = (PFFile *)product[@"image"];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.imageView loadInBackground];
self.priceLabel.text = [NSString stringWithFormat:@"$%d", [product[@"price"] intValue]];
self.likeNumberLabel.text = [NSString stringWithFormat:@"%d",[product[@"likes"] intValue]];
self.textLabel.text = product[@"name"];
self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:17.0f];
self.textLabel.textColor = [UIColor colorWithRed:82.0f/255.0f green:87.0f/255.0f blue:90.0f/255.0f alpha:1.0f];
self.textLabel.shadowColor = [UIColor colorWithWhite:1.0f alpha:0.7f];
self.textLabel.shadowOffset = CGSizeMake(0.0f, 0.5f);
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.numberOfLines = 2;
if ([product[@"hasSize"] boolValue]) {
self.sizeButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.sizeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.sizeButton setTitle:NSLocalizedString(@"Select Size", @"Select Size") forState:UIControlStateNormal];
[self.sizeButton setTitleColor:[UIColor colorWithRed:95.0f/255.0f green:95.0f/255.0f blue:95.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[self.sizeButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0f, 16.0f, 0.0f, 0.0f)];
self.sizeButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16.0f];
[self.sizeButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.sizeButton.titleLabel.textColor = [UIColor colorWithRed:95.0f/255.0f green:95.0f/255.0f blue:90.0f/255.0f alpha:1.0f];
self.sizeButton.titleLabel.shadowOffset = CGSizeMake(0.0f, 0.5f);
UIImage *sizeImage = [UIImage imageNamed:@"DropdownButton.png"];
UIImage *sizePressedImage = [UIImage imageNamed:@"DropdownButtonPressed.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(sizeImage.size.height/2, sizeImage.size.width/2, sizeImage.size.height/2, sizeImage.size.width/2);
[self.sizeButton setBackgroundImage:[sizeImage resizableImageWithCapInsets:insets] forState:UIControlStateNormal];
[self.sizeButton setBackgroundImage:[sizePressedImage resizableImageWithCapInsets:insets] forState:UIControlStateHighlighted];
UIImage *arrowImage = [UIImage imageNamed:@"Arrow.png"];
UIImageView *arrowView = [[UIImageView alloc] initWithImage:arrowImage];
arrowView.frame = CGRectMake(140.0f, (40.0f - arrowImage.size.height)/2.0f, arrowImage.size.width, arrowImage.size.height);
[self.sizeButton addSubview:arrowView];
[self addSubview:self.sizeButton];
}
}
这段代码应该可以做到这一点
- (void)next:(UIButton *)button {
NSLog(@"nextButton");
PFQuery *query = [PFQuery queryWithClassName:@"Item"];
PFObject *object = [self.objects objectAtIndex:button.tag];
[query whereKey:@"objectId" equalTo:[object objectId]];
if ([[likesDict objectForKey:object.objectId] boolValue] == NO) {
[likesDict setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%@",object.objectId]];
[button setTitle:@"unlike" forState:UIControlStateNormal];
[self likeToObject:YES object:object];
} else if ([[likesDict objectForKey:object.objectId] boolValue] == YES) {
[likesDict setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%@",object.objectId]];
[button setTitle:@"like" forState:UIControlStateNormal];
[self likeToObject:NO object:object];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[likesDict writeToFile:[documentsDirectory stringByAppendingPathComponent:@"likes.plist"] atomically:YES];
}我花了一点时间编辑您的问题,以清理代码和英语。我还发现您正在初始化orderButton
以及表视图初始化中的其他按钮。每个按钮都应该是新创建的表视图单元格实例!将按钮分配给新初始化的UIButton
实例,并设置视图属性,所有这些都在cellforrowatinexpath:
中。原因是,当您更改按钮的不透明度时,该按钮被指定给多个表视图单元格,因此其他单元格也会更改。第四个表格视图单元格正在更改(我可能错了)的原因是,可能您必须滚动到该单元格,因此它正在使用该不透明度集重新加载视图
此外,我认为将这些设置写入字典并通过plist将其保存在文件系统中是一种非常糟糕的设计。我要做的是子类PFObject
,并拥有自己的自定义类,该类存储一个布尔属性,如isLiked
,这样将值直接存储到指针,当您从数组中检索对象并计算isLiked
(然后可以更改该属性)时,您可以评估以下操作/步骤.我看不出更改按钮标题会有什么效果,他强调的问题是其他表视图单元格受到不同单元格上的操作的影响。编辑:你好,艾丹!