在自定义UITableView中创建对单元格的引用

时间:2014-12-13 21:11:21

标签: ios objective-c xcode

我创建了一个customTableCell类,它在我的FirstViewController中用作构造UITableView的格式化程序。所有相关课程的代码将在下面提供。

我正在尝试做什么:

我试图更改regularBubbleCostLabel的值,它是customTableCell类的属性。我遇到的问题是我无法引用UITableView中显示的特定单元格。

如何创建对UITableView中显示的每个customTableCell的引用?

customTableCell.h

#import <UIKit/UIKit.h>

@interface customTableCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *primaryImageView;
@property (strong, nonatomic) IBOutlet UILabel *upgradeNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (strong, nonatomic) IBOutlet UIImageView *regularCurrencyIcon;
@property (strong, nonatomic) IBOutlet UILabel *regularBubbleCostLabel;

@end

customTableCell.m

#import "customTableCell.h"

@implementation customTableCell

@synthesize primaryImageView = _primaryImageView;
@synthesize upgradeNameLabel = _upgradeNameLabel;
@synthesize descriptionLabel = _descriptionLabel;
@synthesize regularBubbleCostLabel = _regularBubbleCostLabel;

@end

FirstViewController.h

#import <UIKit/UIKit.h>
#import "RWGameData.h"
#import "customTableCell.h"

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UILabel *regularBubbleLabel;
@property (strong, nonatomic) IBOutlet UILabel *premiumBubbleLabel;

@property (strong, nonatomic) IBOutlet UIImageView *regularBubbleIcon;
@property (strong, nonatomic) IBOutlet UIImageView *premiumBubbleIcon;

@property (strong, nonatomic) IBOutlet UINavigationBar *navBar;

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
{
    NSArray *upgrades;
    NSArray *thumbnails;
    NSArray *descriptions;
    NSArray *megaBubbleUpgradeFees;
    NSInteger rowID;
    NSInteger cellCount;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Upgrades" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

    upgrades = [dict objectForKey:@"UpgradeStrings"];
    thumbnails = [dict objectForKey:@"UpgradeImages"];
    descriptions = [dict objectForKey:@"UpgradeDescriptions"];
    megaBubbleUpgradeFees = [dict objectForKey:@"MegaBubbleUpgradeFee"];

    _regularBubbleLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].regularBubbleCount];
    _premiumBubbleLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].premiumBubbleCount];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [upgrades count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Handles appearance of cells in table.

    static NSString *TableIdentifier = @"TableCell";

    //customTableCell *cell = (customTableCell *)[tableView dequeueReusableCellWithIdentifier:TableIdentifier];
    customTableCell *cell = (customTableCell *)[tableView dequeueReusableCellWithIdentifier:TableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.primaryImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.upgradeNameLabel.text = [upgrades objectAtIndex:indexPath.row];
    cell.descriptionLabel.text = [descriptions objectAtIndex:indexPath.row];
    cell.regularCurrencyIcon.image = [UIImage imageNamed:@"megaBubbleLarge30.png"];

    cell.regularBubbleCostLabel.text = [NSString stringWithFormat:@"%@", megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    rowID = indexPath.row;
    [self makePayment:self];
}

- (IBAction)makePayment:(id)sender {
    UIAlertView *messageAlert;

    if (rowID == 0) {
        if ([RWGameData sharedGameData].regularBubbleCount >= [megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier] intValue]) {
            //NSLog(@"Balance: %li | Cost: %@ |-> Sufficient amount!", [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
            if ([RWGameData sharedGameData].megaBubblePopValue <= 1) {
                [RWGameData sharedGameData].megaBubblePopValue++;
            } else {
                [RWGameData sharedGameData].megaBubblePopValue *= 2;
            }

            [[RWGameData sharedGameData] save];
            NSLog(@"New Pop Value: %i", [RWGameData sharedGameData].megaBubblePopValue);
        } else {
            messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil]; [messageAlert show];
        } NSLog(@"Cell ID: %li | Balance: %li | Cost: %@", rowID, [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
    } else if (rowID == 1) {
        if ([RWGameData sharedGameData].regularBubbleCount >= [megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier] intValue]) {
            NSLog(@"Balance: %li | Cost: %@ |-> Sufficient amount!", [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
        } else {
            messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil]; [messageAlert show];
        } NSLog(@"Cell ID: %li | Balance: %li | Cost: %@", rowID, [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
    } else if (rowID == 2) {
        if ([RWGameData sharedGameData].regularBubbleCount >= [megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier] intValue]) {
            NSLog(@"Balance: %li | Cost: %@ |-> Sufficient amount!", [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
        } else {
            messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil]; [messageAlert show];
        } NSLog(@"Cell ID: %li | Balance: %li | Cost: %@", rowID, [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
    } else if (rowID == 3) {
        if ([RWGameData sharedGameData].regularBubbleCount >= [megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier] intValue]) {
            NSLog(@"Balance: %li | Cost: %@ |-> Sufficient amount!", [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
        } else {
            messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil]; [messageAlert show];
        } NSLog(@"Cell ID: %li | Balance: %li | Cost: %@", rowID, [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
    } else if (rowID == 4) {
        if ([RWGameData sharedGameData].regularBubbleCount >= [megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier] intValue]) {
            NSLog(@"Balance: %li | Cost: %@ |-> Sufficient amount!", [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
        } else {
            messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil]; [messageAlert show];
        } NSLog(@"Cell ID: %li | Balance: %li | Cost: %@", rowID, [RWGameData sharedGameData].regularBubbleCount, megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]);
    }
}

@end

3 个答案:

答案 0 :(得分:2)

您不会以这种方式主动驱动UITableView中的单个单元格。您可以驱动表使用的dataSource。

dataSource应警告表格数据已更改。 (检查Xcode使用Core Data为项目创建的示例项目中的fetchedResultsController代码。)

您可以做几件事来让表和单元格更新:

  • 调用[myTable reloadData](最粗糙,但很少有单元格容易)
  • 调用beginUpdate,发出更改信号,调用endUpdate(fetchedResultsController方式)
  • 对于自定义单元格,他们可以将KVO或NSNotificationCenter与数据源对象一起使用,并在发出信号时自行更新。当细胞被回收时,他们需要取消订阅。重新使用时,他们再次订阅。 (如果它们不在屏幕上,则不需要更新。)

对于前两个,该表将开始为所需的单元调用'cellForRowAtIndexpath'。

答案 1 :(得分:0)

您可以使用-(UITableViewCell *)tableView:cellForRowAtIndexPath:tableView检索特定单元格。只需使用indexPathForRow:inSection:为您要引用的单元格提供相关索引。

答案 2 :(得分:0)

问题解决了。

我继续前进并做了一些非常原始的事情,但它确实有效。我确信这有很多不足之处,但我可以将其更新为将来更可靠。

在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,我添加了以下内容:

if (indexPath.row == 0) {
    cell.regularBubbleCostLabel.text = [NSString stringWithFormat:@"%@", megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]];
} ...