2014-06-17 1 views
0

제목이 혼란스럽게 들릴 경우 죄송합니다. 하지만 테이블보기가있는 응용 프로그램을 만들려고합니다. 각 행에는 고유 한 점 집합이있는 자체 버튼이 있습니다. 나는 버튼이 특정 금액의 포인트를 카운터에 더하고 싶어한다. 그러나, 선택한 행의 NSArray의 int에 액세스하는 방법을 모르겠습니다. 다음은 코드입니다.선택한 행의 NSArray에서 int에 액세스하는 방법?

@interface FBViewController() 

@end 

@implementation FBViewController { 

    NSArray *tablePoints; 
    NSArray *tablePointLabel; 
    int counter; 

} 

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

    //A table that displays the points on the buttons 
    tablePointLabel = [NSArray arrayWithObjects: 
        @"10", 
        @"10", 
        @"10", 
        @"20", 
        @"20", 
        @"20", 
        @"30", 
        @"30", 
        @"50", 
        @"70", 
        @"100", 
        @"300", 
        @"300", 
        nil]; 

    //table of ints (points) 
    tablePoints = [NSArray arrayWithObjects: 
        @10, 
        @10, 
        @10, 
        @20, 
        @20, 
        @20, 
        @30, 
        @30, 
        @50, 
        @70, 
        @100, 
        @300, 
        @300, 
        nil]; 

    counter = 0; 

} 

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [tableData count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *simpleTableIdentifier = @"FBCell"; 

    FBCell *cell = (FBCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

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


    UIButton *pointButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    pointButton.frame = CGRectMake(250.0f, 5.0f, 75.0f, 30.0f); 
    [pointButton setBackgroundColor:[UIColor greenColor]]; 
    [pointButton setTitle:[tablePointLabel objectAtIndex:indexPath.row] forState:UIControlStateNormal]; 
    [cell addSubview:pointButton]; 
    [pointButton addTarget:self 
        action:@selector(addPoint:) 
       forControlEvents:UIControlEventTouchUpInside]; 


    return cell; 
} 

-(IBAction)addPoint:(id)sender { 


    counter + [tablePoints objectAtIndex:indexPath.row] = counter; 
    **//This is where I have an issue. indexPath.row does not work for int arrays. what do i do???** 


} 

답변

0

많은이

나의 추천 대신 나는 각 테이블 행의 높이/폭과 동일하다 같은데요 것을 구현하는 것입니다 (A있는 UIButton의 인스턴스이다 (rmaddy가 제공하는 제안을 포함)을 개선하기 위해

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
//increments counte 
counter += [tablePoints[indexPath.row] intValue]; 
} 

또한, 열 라벨 단지 UILabel의 막대기 및 (있는 UITableViewCell *)를 tableView 위해 (jQuery과 *)를 tableView의 cellForRowAtIndexPath (NSIndexPath *) indexPath 배열 값에 따라 라벨을 설정한다. 사실, 두 개의 동일한 배열을 선언 할 필요가 없으며 동일한 배열을 다시 사용합니다.

+0

당신과 scottdev에게 감사드립니다! 대신이 방법을 사용했습니다. 그것은 더 단순 해 보였다. 고맙습니다!! – DREW1990

+0

우리 모두는 어딘가에서 시작해야합니다. 기꺼이 도와주세요. (답변을 수락하는 것을 잊지 마세요 :) – timothykc

0

NSArray는 값이 아닌 객체를 저장합니다. 귀하의 경우 그것은 NSNumber 개체를 보유하고 차례로 값을 보유합니다.

indexPath.row를 사용하여 NSNumber를 가져온 다음 값을 가져옵니다.

NSNumber *numberObject = [tablePoints objectAtIndex:myIndex]; 
int myInt = [numberObject intValue]; // NSNumber covers a range of different types of values 

하나의 질문은 indexPath.row를 얻는 곳이며 그 시점에서 유효합니다. 방법

+0

사실, 그의 배열에는 NSString 객체가 있습니다. –

+0

@OwenHartnett 아니요, 'tablePoints' 배열은 NSNumber 객체를 포함합니다. – rmaddy

+0

실수로, tablePointLabel 배열 만 보았고 그 배열을 사용하고 있다고 가정했습니다. 배열은 중복 값을 보유합니다. 그는 실제로 하나만 필요합니다. –

관련 문제