【发布时间】:2014-08-10 17:25:22
【问题描述】:
我有一个应用程序,用户可以在其中喜欢和不喜欢我的表格视图中的不同项目。每个表格视图单元格都有一个类似按钮,单击该按钮时会执行类似操作,该操作通过增加我保存的字典中的类似计数来记录。然后,用户可以再次单击该按钮并与该行不同,从而减少点赞计数。当点赞按钮被点击时,会调用如下方法:
- (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];
}
}
【问题讨论】:
标签: ios uitableview ios7 uibutton