UITableView调用另一个UITableView cellForRowAtIndexPath

时间:2014-12-10 06:50:42

标签: ios objective-c iphone uitableview

我有两个带有tableview的UIViewControllers。当第一个单元格在第二个UIViewController中加载时,它会调用同一个类中的cellForRowAtIndexPath,但是当它加载第二个单元格时,它会调用第一个viewControllers cellForRowAtIndexPath

我的代码如下:

SecondViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationsTableViewCell *cell = [self.notificationsTableView dequeueReusableCellWithIdentifier:@"NotificationCell"];

if(cell == nil)
{
    cell = [[NotificationsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NotificationCell"];
}

NSMutableDictionary *cellData = [self.databaseCall transactionFromDatabase:indexPath.row];
NSLog(@"%@", cellData);

cell.goalNameLabel.text = [cellData objectForKey:@"categoryName"];
NSString *cardTypeId = [cellData objectForKey:@"cardTypeId"];
NSString *tipsId = [cellData objectForKey:@"tipsId"];

if([self.defaultCardTypeId containsObject:cardTypeId])
{
    NSUInteger index = [self.defaultCardTypeId indexOfObject:cardTypeId];
    [self.defaultCardTypeId replaceObjectAtIndex:index withObject:cardTypeId];
}

else{
    [self.defaultCardTypeId addObject:cardTypeId];
}

if([self.defaultTipId containsObject:tipsId])
{
    NSUInteger index = [self.defaultCardTypeId indexOfObject:cardTypeId];
    [self.defaultTipId replaceObjectAtIndex:index withObject:cardTypeId];
}

else{
    [self.defaultTipId addObject:tipsId];
}

if([cardTypeId isEqualToString:@"1"])
{
    UIImage *cellImage = [UIImage imageNamed:@"icon2.jpg"];
    cell.cardTypeImage.image = cellImage;
    cell.cardTypeLabel.text = @"GOOD TO KNOW";
    cell.cardTypeLabel.textColor = [UIColor colorWithRed:252/255.0 green:171/255.0 blue:19/255.0 alpha:1];
}

if([cardTypeId isEqualToString:@"2"])
{
    UIImage *cellImage = [UIImage imageNamed:@"icon1.jpg"];
    cell.cardTypeImage.image = cellImage;
    cell.cardTypeLabel.text = @"TO CONSIDER";
    cell.cardTypeLabel.textColor = [UIColor colorWithRed:0/255.0 green:191/255.0 blue:243/255.0 alpha:1];
}
cell.notificationCard.layer.cornerRadius = 5;

// Configure the cell...

return cell;
}

FirstViewController:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoalsCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoalsListCell" forIndexPath:indexPath];
    if(cell == nil)
    {
        cell = [[GoalsCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoalsListCell"];
    }

    NSInteger indexOfCategory = [self.databaseCall.arrColumnName indexOfObject:@"CategoryName"];
    NSInteger indexOfImage = [self.databaseCall.arrColumnName indexOfObject:@"CategoryImage"];
    NSInteger indexOfActive = [self.databaseCall.arrColumnName indexOfObject:@"coulmn"];

    //Assigning the contents of cell
    cell.goalName.text = [NSString stringWithFormat:@"%@", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]];
    NSString *categoryImage = [NSString stringWithFormat:@"%@", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfImage]];
    NSString *activeStatus = [NSString stringWithFormat:@"%@", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfActive]];

    UIImage *cellImage = [UIImage imageNamed:categoryImage];

    cell.goalImage.image = cellImage;
    [cell.favouriteButton addTarget:self action:@selector(favouriteButtonPressed:) forControlEvents:UIControlEventTouchDown];

    NSMutableString *selectedRowImage = [[NSMutableString alloc] initWithString:@""];

    //Checking whether the category is selected by user or not
    if([activeStatus isEqualToString:@"yes"])
    {
        selectedRowImage = [NSMutableString stringWithFormat:@"starsel.png"];
    }
    else
    {
        selectedRowImage = [NSMutableString stringWithFormat:@"stardef.png"];
    }

    UIImage *favouriteIconImage = [UIImage imageNamed:selectedRowImage];
    [cell.favouriteButton setBackgroundImage:favouriteIconImage forState:UIControlStateNormal];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Configure the cell...

    return cell;
    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

首先,我会对这个愚蠢的问题抱歉。 问题是由@ Paulw11,@ Onik IV,@ Kannan Vora指定的tableview数据源引起的。 secondViewController tableView的数据源为firstViewController