저는 FirstViewController에서 UITableView를 생성하기위한 포맷터 역할을하는 customTableCell 클래스를 만들었습니다. 모든 관련 클래스의 코드는 아래에 제공됩니다. 나는 customTableCell 클래스의 속성 인 regularBubbleCostLabel의 값을 변경하려고 시도하고사용자 정의 UITableView에서 셀에 대한 참조 만들기
: 내가 시도하고 무엇
. 내가 가지고있는 문제는 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
당신은 각 셀에 대한 참조를 만들 싶지 않아, 때문에 : (NSIndexPath *) indexPath, 나는 다음과 같은 추가 해당 셀은 테이블 뷰의 다른 위치에서 재사용됩니다. 데이터 모델에 레이블의 값을 보유하는 일부 특성이 있어야하고 indexPath를 기반으로 표를 채울 수 있어야합니다. BTW, 당신은 그 "@ 합성"진술을 더 이상 필요하지 않습니다. 당신은 자동으로 (그리고 지금 당분간) 가지고 있습니다. – rdelmar
클래스 메서드를 통해 싱글 톤 속성에 액세스하는 것이 좋습니다. [RWGameData sharedGameData] .regularBubbleCount 톤 대신에 [RWGameData regularBubbleCount] –