2 UITableView在UIViewController互相影响

我已经在这里约3个小时了,我终于把毛巾扔给你们了。 我有2 UITableView在我看来,其中之一是一个论坛董事会留下意见,什么不是,第二个UITableView是用来提人。 我遇到的问题是,即使我只想更新tableview counts中的一个,两个tableview counts受到影响。

我在viewdidload设置委托/数据源

 self.mentionTableView.delegate = self; self.mentionTableView.dataSource = self; self.mentionTableView.tag = 1; self.forumTable.delegate = self; self.forumTable.dataSource = self; self.forumTable.tag = 2; 

如果我真的不加载feedArray这是哪里forumTable获取其数据,提及部分完美的作品。 我想通了,如果matchedNames.count < feedArray.count一切工作正常,但如果matchedNames.count > feedArray.count它崩溃,这是我得到的错误

 -[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1) 

这意味着我只有1个项目feedArraymatchedNames有不止一个。

代替

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

我已经使用这些代码来试图使它工作,甚至没有任何事情,甚至做了他们的组合。

1)

 if ([tableView isEqual: mentionTableView]){ NSLog(@"%@",matchedNames); NSLog(@"%i",matchedNames.count); return [matchedNames count]; } else if([tableView isEqual:forumTable]){ return [feedArray count]; } 

2)

 if (tableView == mentionTableView){ NSLog(@"%@",matchedNames); NSLog(@"%i",matchedNames.count); return [matchedNames count]; } else if(tableView == commentTable){ return [feedArray count]; } 

3)

  if (tableView.tag == 1){ NSLog(@"%@",matchedNames); NSLog(@"%i",matchedNames.count); return [matchedNames count]; } else if(tableView.tag == 2){ return [feedArray count]; } 

而且我知道我不需要做else if但是我只是想具体到可能避免这个问题。

这是我的cellForRowAtIndexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView.tag == 1){ CGAffineTransform transform = CGAffineTransformMakeRotation(3.14); static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.transform = transform; cell.textLabel.text = [self.matchedNames objectAtIndex:indexPath.row]; return cell; } else { static NSString *CellIdentifier = @"commentCell"; commentsCustomCell *cell =(commentsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[commentsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; NSDictionary *feedPost = [feedArray objectAtIndex:indexPath.row]; NSString *checkIFempty = [feedPost objectForKey:@"isForum_empty"]; if (![checkIFempty isEqualToString:@"1"]) { cell.noCommentsLabel.text = nil; } else { cell.noCommentsLabel.text = @"No Comments"; cell.forumComment = nil; } NSString *username =[feedPost objectForKey:@"Username"]; NSString *final_time =[feedPost objectForKey:@"final_time"]; NSString *userIDforPic =[feedPost objectForKey:@"UserID"]; finalComment = [feedPost objectForKey:@"comment"]; cell.forumComment.text = finalComment; return cell; } } 

如果我的问题令人困惑,我很抱歉,我已经睡了36个小时。 谢谢你看我的问题!

你真的真的应该考虑重构你的代码,并在不同的viewController中分开这两个tableViews。

例如,您可以在您的主视图控制器中使用containerViews,并将tableview放在容器视图控制器中。

TableView委托使用了大量的代码,并且将不同的tableViews设置为同一个控制器将总是这样做,搞乱了代码并给出了比应该更多的麻烦。

有几种技术可以用来帮助清理代码,并且可以减less混乱的解决scheme。 我会解释这一点。

第一个简单的步骤就是创build一个属性或者方法,根据tableView或者标签为你提供数据的数组:

 - (NSArray *)dataForInteger:(NSUInteger)value { return @[self.matchedNames, self.feedArray][value]; } // you can also make this a readonly property for easier access 

为了使用上面的方法,你需要确保需要matchedNames的表的标签是0feedArray1 (或者你可以使用一个简单的if/else语句,并使标签无论你想要的。

现在看这是多么容易,这使得你的未来逻辑!

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [[self dataForInteger:tableView.tag] count]; } 

而已!

接下来,您可能会创build一个如下所示的方法:

 - (UITableViewCell *)emptyCellForTableView:(UITableView *)tableView { NSString *cellIdentifier = @[@"CellIdentifierONe", @"CellIdentifierTWO"][tableView.tag]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { // the only messy looking logic if (!tableView.tag) { // tableView.tag == 0 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; CGAffineTransform transform = CGAffineTransformMakeRotation(3.14); cell.transform = transform; } else { // make sure "CommentsCustomCell" is a class and not an instance (like your example) cell = [[CommentsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } } return cell; } 

然后在你的tableView:cellForRowAtIndexPath:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self emptyCellForTableView:tableView]; NSArray *dataArray = [self dataForInteger:tableView.tag]; // setup all custom UITableViewCell's to have a property or setter "data" if ([cell respondsToSelector:@selector(setData:)]) { // I will explain this a bit later cell.data = dataArray[indexPath.row]; } else { // based on your example this was an NSString array self.textLabel.text = dataArray[indexPath.row]; } } 

现在cell.data = dataArray[indexPath.row]; 是苹果公司的例子如何设置自定义UITableViewCells数据。 只需要创build一个名为data的属性,它的setter看起来像这样(对于你的例子):

 // CommentsCustomCell.m - (void)setData:(id /*or custom class, in this case you use NSDictionary*/)data { _data = data; // NTOE: I just copied your example above, I am sure there is a nicer way to do this but you get the idea. NSDictionary *feedPost = (NSDictionary *)data; NSString *checkIFempty = [feedPost objectForKey:@"isForum_empty"]; if (![checkIFempty isEqualToString:@"1"]) { cell.noCommentsLabel.text = nil; } else { cell.noCommentsLabel.text = @"No Comments"; cell.forumComment = nil; } NSString *username =[feedPost objectForKey:@"Username"]; NSString *final_time =[feedPost objectForKey:@"final_time"]; NSString *userIDforPic =[feedPost objectForKey:@"UserID"]; finalComment = [feedPost objectForKey:@"comment"]; cell.forumComment.text = finalComment; } 

这样的解决scheme如此美丽的原因是,如果你想使用不同的自定义UITableViewCell所有你需要做的是改变空单元格创build方法创build该类,这基本上是(这也允许你使你所有的小区的IBOutlet是私人的!)。 只要新的类有一个data属性,你的主控制器不关心! 它只是通过发送数据。 这也可以让你适当地扩展它。 如果所有的tableView使用的自定义单元格,你可以很容易地在同一个viewController有十几个tableviews,而不需要太多额外的逻辑(只需要添加数组,设置标签,并确保单元格创build方法创build适当的单元格!

我input这个很快,所以可能有一些遗漏的逻辑或错别字。 如果他们指出,我会解决。 祝你好运。