2014-12-13 4 views
-3

저는 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 
+0

당신은 각 셀에 대한 참조를 만들 싶지 않아, 때문에 : (NSIndexPath *) indexPath, 나는 다음과 같은 추가 해당 셀은 테이블 뷰의 다른 위치에서 재사용됩니다. 데이터 모델에 레이블의 값을 보유하는 일부 특성이 있어야하고 indexPath를 기반으로 표를 채울 수 있어야합니다. BTW, 당신은 그 "@ 합성"진술을 더 이상 필요하지 않습니다. 당신은 자동으로 (그리고 지금 당분간) 가지고 있습니다. – rdelmar

+0

클래스 메서드를 통해 싱글 톤 속성에 액세스하는 것이 좋습니다. [RWGameData sharedGameData] .regularBubbleCount 톤 대신에 [RWGameData regularBubbleCount] –

답변

0

에서 특정 셀을 검색하는 데 -(UITableViewCell *)tableView:cellForRowAtIndexPath:을 사용할 수 있습니다. indexPathForRow:inSection:을 사용하여 참조 할 셀의 관련 색인을 제공하기 만하면됩니다.

1

당신은 적극적으로 이런 식으로 UITableView에서 개별 셀을 구동하지 않습니다. 테이블이 사용하는 dataSource를 구동합니다.

dataSource는 데이터가 변경되었음을 테이블에 알립니다. (

  • 전화 [myTable에 reloadData]을 (. 엑스 코드는 코어 데이터를 사용하여 프로젝트에 대해 생성하는 샘플 프로젝트에서 fetchedResultsController 코드를 확인)

    당신은 업데이트 할 테이블과 셀을 얻기 위해 여러 가지 일을 할 수있다 조잡한하지만

  • 전화 BeginUpdate와) 거의 세포 쉬운, 변경, 신호가 데이터 소스 오브젝트와 KVO 또는 NSNotificationCenter를 사용하여 신호를 할 때 자신을 업데이트 할 수 있습니다, 사용자 정의 세포 endUpdate 메소드합니다 (fetchedResultsController 방법)
  • 를 호출합니다. 세포가 재활용되면, 그들은 구독을 취소해야합니다. 재사용하면 다시 구독합니다. (화면 상에 있지 않으면 업데이트가 필요하지 않습니다.)

처음 두 경우, 테이블은 필요한 셀에 대해 'cellForRowAtIndexpath'를 호출하기 시작할 것입니다.

0

문제가 해결되었습니다.

나는 아주 원시적 인 것을 시도했지만 효과가있다. 이것에 대한 많은 단점이 있지만, 미래에는 더 신뢰할 수 있도록 업데이트 할 수 있습니다.에서

- (있는 UITableViewCell *)있는 tableView는 (jQuery과 *)있는 tableView cellForRowAtIndexPath는 :

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

수 있습니다. 행이 ** not ** zero이면 레이블을 지울 필요가 있습니다. –

관련 문제