1

私はこれを約3時間続けてきましたが、ついにタオルを投げています。私の見解では 2UITableViewつあります。1 つはコメントを残すためのフォーラム ボードで、もう 1 つはUITableView人々に言及するために使用されます。私が抱えている問題tableview countsは、テーブルビューの 1 つだけを更新したい場合でも、両方が影響を受けていることです。

デリゲート/データソースを設定しました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.countfeedArray.countmatchedNames.countfeedArray.count

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

つまり、アイテムが 1 つしかなく、複数あることを意味しfeedArrayますmatchedNames

の中に

- (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 時間寝ていません。私の質問を見てくれてありがとう!

4

2 に答える 2

2

コードをリファクタリングし、この 2 つの tableView を異なる viewController に分離することを検討する必要があります。

たとえば、メインのviewController内でcontainerViewsを使用し、tableviewをコンテナのView Controllerに配置できます。

TableViewデリゲートは多くのコードを使用し、異なるtableViewを同じコントローラーに設定すると、常にこのようになり、コードが混乱し、本来よりも多くの問題が発生します。

于 2013-08-09T12:20:20.997 に答える