我有一个应用程序,用户可以在桌面视图中喜欢和不喜欢不同的项目。每个表格视图单元格都有一个相似的按钮,单击该按钮时会执行类似的操作,该操作通过在我保存的字典中递增相似计数来记录。然后,用户可以再次单击该按钮而不是该行,因此减少相同的计数。单击like按钮时,将调用以下方法:
- (void)next:(UIButton *)button
现在我正在使用[button setAlpha:0.1];
(更改不透明度)来表明它是否被喜欢。 这是我的问题:由于某种原因另一个表格视图单元格受到影响/更改,除了目标单元格!因此,例如,如果我调用此方法并单击第1行,另外,第4行将受到影响。
喜欢和不喜欢的地方叫:
- (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];
}
}
答案 0 :(得分:1)
我花了一些时间编辑你的问题来清理代码和英语。我还发现你正在初始化orderButton
以及表视图初始化中的其他按钮。每个按钮应该是一个新创建的表视图单元实例!将按钮分配给新初始化的UIButton
实例,并设置视图属性,所有这些都在cellForRowAtIndexPath:
内。原因是,当您更改按钮的不透明度时,该按钮被分配给多个表视图单元格,因此其他单元格也会更改。第四个表视图单元格正在改变的原因,我可能是错的,也许是你必须滚动到的那个,因此它正在重新加载具有该不透明度集的视图。
此外,我认为将这些设置写入字典并通过plist将其保存在文件系统中是一种非常糟糕的设计。我要做的是子类PFObject
并拥有自己的自定义类,它存储一个布尔属性,如isLiked
,这样值就会直接存储到指针中,你可以评估以下操作/步骤从数组中检索对象并评估isLiked
(然后可以更改)。
答案 1 :(得分:0)
此代码应该这样做
- (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];
}