gpt4 book ai didi

ios - tableView 添加/删除到第二部分 objective-c

转载 作者:行者123 更新时间:2023-11-29 10:49:24 24 4
gpt4 key购买 nike

我的 TableView (iOS) 中有两个部分,第一个部分用于“收藏夹”行。如果您选择 accessoryView,它应该将“objects”数组中的选定对象添加到“favs”数组或从“favs”数组中删除,具体取决于该对象是否已存在于该部分中。

我编写的方法几乎可以工作,但是当我添加第二行,或将其从收藏夹中删除时,应用程序崩溃并出现此错误:由于未捕获的异常 NSRangeException' 而终止应用程序,原因:*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]问题是相应数组中对象的索引不相同,所以我真的不知道如何使它正常工作。

这是一些代码:

-(void)addToFavs:(id)sender{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(@"Tag = %d", gesture.view.tag);
//if favorite section is empty
if ([self.favs isEqualToArray:[@[]mutableCopy]]) {
NSLog(@"adding favorite");
[self.favs addObject:[self.objects objectAtIndex:gesture.view.tag]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:gesture.view.tag]];
[self.iconsFavs addObject:[self.icons objectAtIndex:gesture.view.tag]];
}
//if selected row already exist in favorites array <--HERE IS THE PROBLEM (I THINK)
else if([[self.objects objectAtIndex:gesture.view.tag] isEqualToString:[self.favs objectAtIndex:gesture.view.tag]]){
NSLog(@"removing favorite");
[self.favs removeObjectAtIndex:gesture.view.tag];
[self.subtitlesFavs removeObjectAtIndex:gesture.view.tag];
[self.iconsFavs removeObjectAtIndex:gesture.view.tag];
}else{
NSLog(@"adding favorite");
[self.favs addObject:[self.objects objectAtIndex:gesture.view.tag]];
[self.subtitlesFavs addObject:[self.subtitles objectAtIndex:gesture.view.tag]];
[self.iconsFavs addObject:[self.icons objectAtIndex:gesture.view.tag]];
}
[self.tableView reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// left image
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
if(indexPath.section==0){
image.image=[UIImage imageNamed:[self.iconsFavs objectAtIndex:indexPath.row]];
}else{
image.image=[UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
}

//fav image
UIImageView *fav = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites.png"]];
[fav setFrame:CGRectMake(0, 0, 25, 25)];
[fav setClipsToBounds:YES];
if(!indexPath.section==0) {
fav.image=[UIImage imageNamed:@"unfavorites"];
}

//cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=self.objects[indexPath.row];
cell.detailTextLabel.text =self.subtitles[indexPath.row];
[cell.contentView addSubview:image];
cell.accessoryView = fav;

//Favorites
cell.accessoryView.userInteractionEnabled = YES;
cell.accessoryView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addToFavs:)];
tapped.numberOfTapsRequired = 1;
[cell.accessoryView addGestureRecognizer:tapped];

//favorites section contents
if (indexPath.section == 0)
{
cell.textLabel.text=self.favs[indexPath.row];
[cell.contentView addSubview:image];
cell.detailTextLabel.text =self.subtitlesFavs[indexPath.row];
}
}
return cell;
}

更新1

我在 if 条件中用“isEqualToString”编辑了我的代码,但删除它们仍然不起作用...

最佳答案

与您所拥有的非常相似,但无论如何都要尝试一下:

这是 -cellForRowAtIndexPath: 方法 --

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}

cell.detailTextLabel.textColor = [UIColor lightGrayColor];
//following commented line not needed
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

// left image
UIImageView *image = [[UIImageView alloc] init];
[image setFrame:CGRectMake(7, 7, 30, 30)];
[image.layer setCornerRadius:image.frame.size.width/2];
[image setClipsToBounds:YES];
[image.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[image.layer setBorderWidth:0.3f];
[cell.contentView addSubview:image];

//favorites image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 25, 25)];
[button setBackgroundColor:[UIColor clearColor]];
[button setImage:[UIImage imageNamed:@"unfavorites.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"favorites.png"] forState:UIControlStateSelected];
[button setTag:100]; //tag needed later
[button addTarget:self action:@selector(addToFavs:event:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
[cell setAccessoryView:button];

switch(indexPath.section) {
case 0: { //is first section (favourite)
image.image = [UIImage imageNamed: self.iconsFavs[indexPath.row]];
cell.textLabel.text = self.favs[indexPath.row];
cell.detailTextLabel.text = self.subtitlesFavs[indexPath.row];
[button setSelected:YES];
}
break;
case 1: { //is second section (all clubs)
image.image = [UIImage imageNamed: self.icons[indexPath.row]];
cell.textLabel.text = self.objects[indexPath.row];
cell.detailTextLabel.text = self.subtitles[indexPath.row];

//change state of button (thereby change button image)
if([self.favs containsObject: self.objects[indexPath.row]]) {
[button setSelected:YES];
} else {
[button setSelected:NO];
}
}
break;
}

//separator -- do the following on viewDidLoad instead
//[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];

return cell;
}

这是 -addToFavs:event: 方法 --

//No need for `-accessoryButtonTappedForRowWithIndexPath:` method
//it can be done in your following `-addToFavs:event:` method alone:
-(void)addToFavs:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];

switch(indexPath.section) {
case 0: { //is first section (favourite)
//remove from favourites

//get indexPath for appropraite row in second section
NSUInteger i_indexOfFavInMain = [self.objects indexOfObject:self.favs [indexPath.row]];
NSIndexPath *indexOfFavInMain = [NSIndexPath indexPathForItem:i_indexOfFavInMain inSection:1];

[self.favs removeObjectAtIndex:indexPath.row];
[self.subtitlesFavs removeObjectAtIndex:indexPath.row];
[self.iconsFavs removeObjectAtIndex:indexPath.row];

//handle second section button (for the appropriate row)
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexOfFavInMain];
UIButton *btnTemp = (UIButton *)[cell viewWithTag:100];
[btnTemp setSelected:NO];

//reload first section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;

case 1: { //is second section (all clubs)
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIButton *btnTemp = (UIButton *)[cell viewWithTag:100];

if([self.favs containsObject:self.objects[indexPath.row]]) {
//remove from favourites
[self.favs removeObject:self.objects[indexPath.row]];
[self.subtitlesFavs removeObject:self.subtitles[indexPath.row]];
[self.iconsFavs removeObject:self.icons[indexPath.row]];

[btnTemp setSelected:NO];
} else {
//add to favourites
[self.favs addObject: self.objects[indexPath.row]];
[self.subtitlesFavs addObject: self.subtitles[indexPath.row]];
[self.iconsFavs addObject: self.icons[indexPath.row]];

[btnTemp setSelected:YES];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;
}
}

关于ios - tableView 添加/删除到第二部分 objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21079207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com