이상한 시나리오가 있습니다. 장바구니 화면을 만들고 있습니다. 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];
}
레이블의 텍스트를 변경하고 텍스트 변경을 트리거하는 코드는 무엇입니까? – jancakes
indexPathCounter를 사용하면 모든 셀이 순서대로 빌드 될 것이라고 생각하는 것처럼 보입니다. 그 경우가 아닙니다. cellForRowAtIndexPath : 사용자가 위아래로 스크롤 할 때 코드가 호출됩니다. 따라서 셀의 태그를 사용하려면 [indexPath 행] (1 섹션 만있는 경우)에 태그를 지정해야합니다. 예를 들어 [indexPath row] * 3, [indexPath row] * 3 + 1, [indexPath row] * 3 + 2로 태그를 지정할 수 있습니다. 그러면 testCounter를 호출하면 어떤 셀에서 보낸 사람의 삶. – RobP
설명 할 수있는 코드를 편집 할 수 있습니까? 나는 순간에 혼란스러워 조금 – user3422986