2014-03-29 2 views
1

이상한 시나리오가 있습니다. 장바구니 화면을 만들고 있습니다. UITableView가 있고 모든 행에 더하기 단추와 값이 증가하거나 감소 할 Label이 포함되어 있습니다. 버튼을 누른 사용자에 따라 다릅니다. 이제 행 1의 추가 버튼을 누르면 모든 행에 내 UIlabel의 증가 값이 설정됩니다. 이걸 없애는 방법?cellForRowAtIndexPath의 UILabel 수를 설정합니다.

p.s. cellForRowAtIndex 경로에서 레이블 값을

lblCounterDisplay.text = [NSString stringwithformat:@"%d",counter]; 

으로 설정하고있는 반면 카운터는 기본적으로 0으로 설정되어있는 전역 값입니다.

편집 : 여기 내 전체 코드입니다

// global variables 
int dynamicHeight; 
bool flag=NO; 
int indexPathCounter = 0; 
int displayValue = 0; 

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


table.delegate = self; 
table.dataSource = self; 



arrForSec1 = [[NSMutableArray alloc] initWithObjects:@"test",@"tes",@"test",@"test",@"tes",@"test", nil]; 
} 


// table view delegate methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; //count of section 
} 

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

    return [arrForSec1 count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:MyIdentifier]; 
    } 



    UIButton *btnplus = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, 23, 23)]; 
    [btnplus setTitle:@"+" forState:UIControlStateNormal]; 
    [cell addSubview:btnplus]; 
    [btnplus addTarget:self action:@selector(testCounter:) forControlEvents:UIControlEventTouchUpInside]; 
    btnplus.tag = indexPathCounter; 
    btnplus.backgroundColor = [UIColor blackColor]; 



    UILabel *lblCounter = [[UILabel alloc] initWithFrame:CGRectMake(160, 10, 23, 23)]; 
    lblCounter.text = [NSString stringWithFormat:@"%d",displayValue]; 
    lblCounter.backgroundColor = [UIColor orangeColor]; 
    lblCounter.textAlignment =UITextAlignmentCenter; 
    [cell addSubview:lblCounter]; 


    UIButton *btnMinus = [[UIButton alloc] initWithFrame:CGRectMake(285, 10, 23, 23)]; 
    [btnMinus setTitle:@"+" forState:UIControlStateNormal]; 
    [cell addSubview:btnMinus]; 
    btnMinus.tag = indexPathCounter+1; 
    [btnMinus addTarget:self action:@selector(testCounter:) forControlEvents:UIControlEventTouchUpInside]; 
    btnMinus.backgroundColor = [UIColor blackColor]; 

    indexPathCounter = indexPathCounter+2; 


    return cell; 
} 


-(void)testCounter:(id)sender 
{ 
    NSLog(@"kuch aya ? %ld",(long)[sender tag]); 
    if([sender tag] % 2 == 0) 
    { 
     NSLog(@"plus!"); 
     displayValue = displayValue +1; 

    } 
    else 
    { 
     NSLog(@"its minus!"); 
     if(displayValue != 0) 
    { 
     displayValue = displayValue -1; 

    } 


} 
    [table reloadData]; 
} 
+0

레이블의 텍스트를 변경하고 텍스트 변경을 트리거하는 코드는 무엇입니까? – jancakes

+0

indexPathCounter를 사용하면 모든 셀이 순서대로 빌드 될 것이라고 생각하는 것처럼 보입니다. 그 경우가 아닙니다. cellForRowAtIndexPath : 사용자가 위아래로 스크롤 할 때 코드가 호출됩니다. 따라서 셀의 태그를 사용하려면 [indexPath 행] (1 섹션 만있는 경우)에 태그를 지정해야합니다. 예를 들어 [indexPath row] * 3, [indexPath row] * 3 + 1, [indexPath row] * 3 + 2로 태그를 지정할 수 있습니다. 그러면 testCounter를 호출하면 어떤 셀에서 보낸 사람의 삶. – RobP

+0

설명 할 수있는 코드를 편집 할 수 있습니까? 나는 순간에 혼란스러워 조금 – user3422986

답변

1

귀하의 카운터 변수는 행만큼 지수와 카운터의 NSArray를해야한다.

그런 다음 그 카운터에

lblCounterDisplay.text = [NSString stringWithFormat:@"%@", [counter objectAtIndex:indexPath.row]]; 

편집을 각 행을 일치합니다 :

귀하의 전역 변수는 문제의 주요 루트입니다.

int indexPathCounter = 0; 
int displayValue = 0; 

각 행 수를 NSMutableArray으로 추적해야합니다.

다음과 같이 변경합니다 :

있는 viewDidLoad 명확하게하기 위해서

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    table.delegate = self; 
    table.dataSource = self; 

    arrForSec1 = [[NSMutableArray alloc] 
        initWithObjects:@"test",@"tes",@"test",@"test",@"tes",@"test", nil]; 
    //this is the array that will keep track of each count 
    arrForCounters = [NSMutableArray arrayWithCapacity:[arrForSec1 count]]; 

    for (NSString *testString in arrForSec1) { 
     [arrForCounters addObject:@"0"]; 
    } 

} 

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 
    } 



    UIButton *btnplus = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, 23, 23)]; 
    [btnplus setTitle:@"+" forState:UIControlStateNormal]; 
    [cell addSubview:btnplus]; 
    [btnplus addTarget:self action:@selector(increaseCounter:) forControlEvents:UIControlEventTouchUpInside]; 
    btnplus.tag = indexPath.row; 
    btnplus.backgroundColor = [UIColor blackColor]; 



    UILabel *lblCounter = [[UILabel alloc] initWithFrame:CGRectMake(160, 10, 23, 23)]; 
    lblCounter.text = [NSString stringWithFormat:@"%@", arrForCounters[indexPath.row]]; 
    lblCounter.backgroundColor = [UIColor orangeColor]; 
    lblCounter.textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter; (deprecated!) 
    [cell addSubview:lblCounter]; 


    UIButton *btnMinus = [[UIButton alloc] initWithFrame:CGRectMake(285, 10, 23, 23)]; 
    [btnMinus setTitle:@"-" forState:UIControlStateNormal]; 
    [cell addSubview:btnMinus]; 
    btnMinus.tag = indexPath.row + 1000; 
    [btnMinus addTarget:self action:@selector(decreaseCounter:) forControlEvents:UIControlEventTouchUpInside]; 
    btnMinus.backgroundColor = [UIColor blackColor]; 


    return cell; 
} 

나는이 개 방법으로 당신에게 testCounter 기능을 분할 :

-(void)increaseCounter:(id)sender 
{ 
    int tag = [sender tag]; 
    int counter = [arrForCounters[tag]integerValue]; 

    arrForCounters[tag] = [NSString stringWithFormat:@"%d", counter + 1]; 

    [table reloadData]; 
} 



-(void)decreaseCounter:(id)sender 
{ 
    int tag = [sender tag]; 
    int counter = [arrForCounters[tag - 1000]integerValue]; 


    if (counter!=0) { 
     arrForCounters[tag - 1000] = [NSString stringWithFormat:@"%d", counter - 1]; 

     [table reloadData]; 
    } 

} 

나는 도움이되기를 바랍니다. SAMPLE PROJECT을 작성했습니다.

+0

내 카운터 값은 실제로 핵심 데이터에서 나오지만 괜찮겠습니다. CellForRowAtIndexPath에서 위의 행과 일치하지만 사용자가 추가 버튼을 누르면 입자 행의 lblCounterDisplay에서 증가하는 방법은 무엇입니까? – user3422986

+0

@ user3422986 당신은 더 많은 코드를 공유 할 수 있습니다 – meda

+0

내가 편집 한 pls 모양을 가지고 – user3422986

관련 문제