2 UIViewController中的UITableView相互影响 [英] 2 UITableView in a UIViewController affecting each other
问题描述
我已经花了大约3个小时了,我终于把毛巾扔给所有人了。我的视图中有2个 UITableView
,其中一个是论坛,可以发表评论,什么不可以,第二个 UITableView
用来提人。我遇到的问题是,即使我只想更新其中一个表视图,两个 tableview计数
都受到影响。
I've been going at this for about 3 hours now, and I'm finally throwing in the towel to y'all. I have 2 UITableView
in my view, one of which is a forum board to leave comments and what not, and the second UITableView
is used to mention people. the issue I am having is that both tableview counts
are being affected even if i only want to update just 1 of the tableviews.
我在 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
它崩溃了,这是我得到的错误
and if i actually do not load feedArray
which is where forumTable
gets its data from, the mention portion works perfectly. I've figured out that if matchedNames.count
<feedArray.count
everything works fine, but if matchedNames.count
>feedArray.count
it crashes and this is the error i get
-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)
这意味着我在 feedArray
中只有1个项目,而 matchedNames
有多个项目。
which means that i only have 1 item in feedArray
and matchedNames
has more than one.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
我已经使用这些代码来尝试
i have used these codes to try to make it work and still nothing, ive even done combinations of them.
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
,但我只是想具体说明,以免发生此问题。
and I know I didnt have to do the else if
but I was just trying to be specific to maybe avoid this issue.
这是我的 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小时。感谢您看我的问题!
if my question is confusing i do apologize, I haven't slept 36 hours. thanks for looking at my question!
推荐答案
您真的真的应该考虑重构您的代码,并将这两个tableViews放在不同的viewControllers中。
You really really really should consider to refactor your code and separate this two tableViews in different viewControllers.
例如,您可以在主viewController中使用containerViews并将tableview放入容器视图控制器中。
You can, for example, use containerViews inside your main viewController and put the tableview in the container view controllers.
TableView委托使用了大量代码,并且将不同的tableViews设置到同一控制器将总是这样,从而弄乱了代码,并给程序带来了更多麻烦。
TableView delegates use a lot of code, and set different tableViews to the same controller will always end up like this, messing the code and giving more trouble than it should be.
这篇关于2 UIViewController中的UITableView相互影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!