แสดงจำนวนการคลิกปุ่มในป้ายกำกับสำหรับเซลล์เฉพาะซึ่งเป็นการดูย่อยไปยัง uitableview

ฉันมีปุ่มและป้ายกำกับซึ่งเป็นมุมมองย่อยของ UITableView

ค่าป้ายกำกับเริ่มต้นคือ 0

สิ่งที่ฉันต้องการคือ เมื่อฉันคลิกปุ่มบนเซลล์ใดเซลล์หนึ่ง ฉันต้องการเพิ่มค่าในป้ายกำกับเซลล์เดียวกัน (เป็น 1) และแสดงค่านั้นในป้ายกำกับเดียวกัน

และอีกครั้งที่ฉันคลิกปุ่มเซลล์เดียวกัน ป้ายกำกับในเซลล์นั้นควรเพิ่มขึ้น (เป็น 2) และแสดงค่านั้นในเซลล์เดียวกันใน UITableView

รหัสของฉัน..

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

{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Lbl;
UIButton *btn;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    Lbl =[[UILabel alloc]init];
    [Lbl setFrame:CGRectMake(56, 60, 117, 12)];
    [Lbl setBackgroundColor:[UIColor clearColor]];
    [Lbl setTextAlignment:NSTextAlignmentLeft];
    [Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
    Lbl.tag=indexPath.row;
    [cell.contentView addSubview:Lbl];

    btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"add" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
    [[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
    [btn setFrame:CGRectMake(289, 2, 30, 71)];
    btn.tintColor=[UIColor lightGrayColor];
    btn.tag=indexPath.row;
    [btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btn];
} else {
    Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
    btn =(UIButton *)[cell.contentView viewWithTag:indexPath.row];
}

cell.textLabel.text=@"title";
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

return cell;

}

// button action method
-(void)increaseItemCount:(UIButton *)sender
{    
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(@"row: %d",path.row);
UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:path.row];
showItemCount=[countLbl.text intValue] + 1;
NSLog(@"%d",showItemCount);
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

}

ฉันลองสิ่งนี้ หลังจากคลิกแล้ว ค่าจะแสดงในเซลล์อื่น และเมื่อฉันเลื่อนมุมมองตาราง ค่านั้นจะแสดงในเซลล์ทั้งหมด

ข้อเสนอแนะหรือรหัสใด ๆ


person Community    schedule 08.08.2013    source แหล่งที่มา
comment
ทำไม Lbl.tag=indexPath.row;?   -  person Meera    schedule 08.08.2013
comment
ฉันไม่สามารถรับคุณได้ คุณบอกฉันได้ไหมว่าปัญหาอยู่ที่ไหน   -  person    schedule 08.08.2013
comment
คุณได้รับจำนวนที่ถูกต้อง แต่คุณยุ่งกับเซลล์ตารางหรือไม่? ฉันเดาว่าแท็กของฉลากคือผู้ร้าย   -  person Meera    schedule 08.08.2013


คำตอบ (4)


หากคุณต้องการซอร์สโค้ด ดาวน์โหลดได้จากที่นี่ https://github.com/MasudShuvo/TestCustomTableViewCell

ฉันได้แก้ไขโค้ดตามความต้องการของคุณแล้ว

person shuvo    schedule 08.08.2013
comment
ฉันลองใช้รหัสของคุณแล้ว มันใช้งานได้ เมื่อฉันเพิ่มความสูงของเซลล์เป็น 65 แอปจะยับ มันแสดงข้อผิดพลาดเช่นแอปที่กำลังยุติเนื่องจากข้อยกเว้นที่ไม่ถูกตรวจจับ 'NSRangeException' เหตุผล: '*** -[__NSArrayM objectAtIndex:]: ดัชนี 7 เกินขอบเขต [0 .. 6]' - person ; 08.08.2013
comment
คุณเปลี่ยนความสูงของเซลล์ในโครงการของฉันหรือของคุณ? - person shuvo; 08.08.2013
comment
คุณช่วยบอกฉันว่าจะหยุดการลดค่าได้อย่างไร...เมื่อค่าถึง 0 - person ; 08.08.2013
comment
คุณช่วยบอกวิธีใช้การกดแบบยาวพร้อมกับการคลิกได้ไหม - person ; 08.08.2013
comment
เมื่อฉันรันโครงการของคุณ มันก็ทำงานได้ดี เมื่อนำไปใช้ในโครงการของฉันแอปขัดข้อง มันแสดงข้อผิดพลาดเช่นนี้ -[__NSArrayM objectAtIndex:]: ดัชนี 0 เกินขอบเขตสำหรับอาร์เรย์ว่าง ' - person ; 08.08.2013
comment
ยินดีที่ได้ยินเช่นนั้น :-) ห้องน้ำ - person shuvo; 08.08.2013
comment
สวัสดี ชูโว่ ความช่วยเหลือเล็กๆ น้อยๆ.......วิธีใช้งาน searchBar และ searchDisplayController ในโครงการของคุณ - person ; 09.08.2013

นั่นเป็นเพราะดูเหมือนว่าคุณกำลังใช้ตัวแปรอินสแตนซ์ตัวเดียวในการจัดเก็บ itemCount คุณควรใช้อาร์เรย์เพื่อทราบว่าเซลล์ใดถูกคลิกไปกี่ครั้ง อย่างไรก็ตาม โค้ดค่อนข้างยุ่ง ดังนั้นคุณควรเขียนใหม่ตั้งแต่ต้น

person Vik    schedule 08.08.2013
comment
ใช่ คุณพูดถูก..ฉันกำลังใช้ตัวแปรอินสแตนซ์ตัวหนึ่งเพื่อจัดเก็บจำนวนคลิก คุณสามารถให้รหัสบางอย่างเพื่อแก้ไข - person ; 08.08.2013
comment
คุณสามารถใช้ตัวแปรอินสแตนซ์ NSMutableArray ที่เริ่มต้นด้วย @0 สำหรับทุกดัชนี แล้วคุณมี showItemCount = [...] คุณก็ทำได้ myInstanceVariable[indexPath.row] = @(myInstanceVariable[indexPath.row] + 1); และที่คุณมี countLbl.text = [...] คุณก็เขียนได้ countLbl.text = [NSString stringWithFormat:@"%@",myInstanceVariable[indexPath.row]]; - person Vik; 08.08.2013
comment
บรรทัดนี้แสดงข้อผิดพลาด โดยที่ SelectedCells เป็นแบบ mutablearray เซลล์ที่เลือก[indexPath.row]=@(เซลล์ที่เลือก[indexPath.row] + 1); - person ; 08.08.2013
comment
ใช่ ฉันพิมพ์ไปแบบไม่ได้ตั้งใจ คุณควรพัน selectedCells[indexPath.row] ด้วย [... intValue] เพื่อให้ดูเหมือน = @([selectedCells[indexPath.row] intValue] + 1); - person Vik; 08.08.2013
comment
ตอนนี้แอปของฉันเกิดข้อผิดพลาดในบรรทัดนี้ countLbl.text = [NSString stringWithFormat:@%@,selectedCells[indexPath.row]]; ฉันกำลังใช้บรรทัดนี้ในวิธี cellForRowAtIndexPath - person ; 08.08.2013
comment
[__NSArrayM objectAtIndex:]: ดัชนี 1 เกินขอบเขต [0 .. 0]' - person ; 08.08.2013
comment
ในความคิดเห็นของฉัน ฉันบอกว่าคุณต้องเริ่มต้นอาร์เรย์ด้วย @0 สำหรับทุกดัชนี นั่นหมายความว่าคุณควรดำเนินการบางอย่าง เช่น [selectedCells addObject:@0] ใน for รอบสำหรับจำนวนเซลล์ที่คุณมี หรือคุณสามารถทำใน cellForRowAtIndexPath ตรวจสอบด้วย if([selectedCells count] <= indexPath.row) [selectedCells addObject:@0] ก่อนและแทนการเพิ่มเนื้อหา - person Vik; 08.08.2013

indexPath.row เริ่มต้น 0 ถึง n เราไม่ได้ตั้งค่าแท็ก 0 สำหรับป้ายกำกับ และคุณใช้ตัวแปรเดี่ยว showItemCount ที่ใช้เพื่อกำหนดข้อความใน countLbl ตาราง ดูเซลล์ที่ใช้ซ้ำเมื่อจะปรากฏขึ้น ให้ใช้อาร์เรย์เพื่อจัดเก็บ showItemCount สำหรับแต่ละเซลล์

person NANNAV    schedule 08.08.2013

โปรดดำเนินการดังต่อไปนี้:

@implementation ViewController

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   // UILabel *Lbl;
   // UIButton *btn;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UILabel *Lbl =[[UILabel alloc]init];
        [Lbl setFrame:CGRectMake(56, 60, 117, 12)];
        [Lbl setBackgroundColor:[UIColor clearColor]];
        [Lbl setTextAlignment:NSTextAlignmentLeft];
        [Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
        Lbl.tag=indexPath.row;
        Lbl.text=[NSString stringWithFormat:@"%i",0];
        [cell.contentView addSubview:Lbl];

        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"add" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
        [[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
        [btn setFrame:CGRectMake(289, 2, 30, 71)];
        btn.tintColor=[UIColor lightGrayColor];
        btn.tag=indexPath.row;
        [btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];

 NSLog(@"Selected Index:%i",indexPath.row);   
    } else {

       UILabel *Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
      //UIButton *btn =(UIButton *)[cell viewWithTag:indexPath.row];

        if(indexPath.row==selectedIndex)
        {
             NSLog(@"Selected Index in CellForRowAtIndexPath:%i",indexPath.row);
            Lbl.text=[NSString stringWithFormat:@"%i",[Lbl.text intValue]+1];
        }

    }

   // cell.textLabel.text=@"title";
    //countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

    return cell;
}

-(void)increaseItemCount:(UIButton *)sender
{
    selectedIndex=sender.tag;
    [tblView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Selected Index:%i",indexPath.row);
}

ฉันหวังว่านี่จะช่วยคุณได้

person hpp    schedule 08.08.2013
comment
ฉันต้องการเพิ่มจำนวนการคลิกปุ่ม - person ; 08.08.2013
comment
ฉันพยายามแล้ว แต่จำนวนเพิ่มขึ้นโดยอัตโนมัติเมื่อฉันเลื่อนดูตาราง และจำนวนเพิ่มขึ้นในเซลล์สุ่ม - person ; 08.08.2013