次のようなカスタムセルを作成しました
この「+」記号では、チケット数が増加し、「-」記号では減少します。チケット数が 0 の場合は「-」が無効になり、チケット数が 4 の場合は「+」が無効になります。今のところ問題なく動作しています。問題は、4行目の「+」ボタンをクリックするとテーブルのフレームサイズを変更し、4行目の「-」をクリックするとフレームサイズを元に戻したいです。そうするとき、他の行の「-」機能がランダムに変更されます。何が間違っているのか教えてください。ありがとうこれが私のコードです
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
cell
= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
UILabel *labelOne = [[UI
Label alloc]initWithFrame:CGRectMake(70, 10, 100, 20)];
labelOne.tag = 100;
labelOne.backgroundColor=[UIColor clearColor];
labelOne.font=[UIFont systemFontOfSize:14];
[cell.contentView addSubview:labelOne];
[labelOne release];
minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];
minusbutton.tag=104;
[minusbutton setEnabled:NO];
minusbutton.backgroundColor=[UIColor clearColor];
[minusbutton addTarget:self action:@selector(DecreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[minusbutton setTitle:@"-" forState:UIControlStateNormal];
[minusbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[minusbutton setBackgroundImage:[UIImage imageNamed:@"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:minusbutton];
[minusbutton release];
UILabel *quantity = [[UILabel alloc]initWithFrame:CGRectMake(170, 5, 20, 20)];
quantity.tag = 103;
quantity.backgroundColor=[UIColor clearColor];
quantity.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:quantity];
[quantity release];
addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];
addbutton.tag=105;
addbutton.backgroundColor=[UIColor clearColor];
[addbutton addTarget:self action:@selector(IncreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[addbutton setTitle:@"+" forState:UIControlStateNormal];
[addbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[addbutton setBackgroundImage:[UIImage imageNamed:@"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:addbutton];
[addbutton release];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(240, 10, 80, 20)];
labelTwo.tag = 101;
labelTwo.backgroundColor=[UIColor clearColor];
labelTwo.font=[UIFont systemFontOfSize:14];
labelTwo.textColor=[UIColor colorWithRed:0.25098 green:0.447059 blue:0.07451 alpha:1];
[cell.contentView addSubview:labelTwo];
[labelTwo release];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(70, 30, 230, 30)];
label3.tag = 102;
label3.backgroundColor=[UIColor clearColor];
label3.numberOfLines=2;
label3.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:label3];
[label3 release];
}
}
UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100];
NSString *ss2=[[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray objectAtIndex:indexPath.row ]tit] ];
labelOne.text = ss2;
UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101];
NSString *string2=[[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray objectAtIndex:indexPath.row ]price] ];
labelTwo.text =string2;
UILabel *label3 = (UILabel *) [cell.contentView viewWithTag:102];
label3.text=[[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray objectAtIndex:indexPath.row ]desc ] ];
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:@"%d",[[eventTicketListArray objectAtIndex:indexPath.row ]qty ] ];
}
return cell;
}
+ボタンのクリック方法
-(void)IncreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
for(UILabel *lbl in [currentCell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
{
str = [lbl.text intValue];
str++;
eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
eventTicketList.qty=str;
lbl.text=[NSString stringWithFormat:@"%d",str];
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
{
if (str==4) {
[btn setEnabled:NO];
}
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
{
[btn setEnabled:YES];
}
}
if (row==3) {
ticketTable.frame=CGRectMake(0, 0, 320, 70);
}
[currentCell setNeedsDisplay];
}
- ボタンクリックの方法
-(void)DecreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
for(UILabel *lbl in [currentCell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
{
str = [lbl.text intValue];
str--;
eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
eventTicketList.qty=str;
lbl.text=[NSString stringWithFormat:@"%d",str];
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
{
if (str<=0) {
[btn setEnabled:NO];
}
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
{
[btn setEnabled:YES];
}
}
if (row==3){
ticketTable.frame=CGRectMake(0, 0, 320, 200);
}
[currentCell setNeedsDisplay];
}