按钮 UITableView 设置标签的问题

发布于 2024-12-13 02:33:58 字数 5173 浏览 1 评论 0原文

我在分区表视图单元格内有两个按钮,分别是“拇指向上”和“拇指向下”。最初,两个按钮的图像均未选择。当我选择“竖起大拇指”按钮时,其图像将变为“竖起大拇指”,而其他图像则变为“竖起大拇指”,未选择,反之亦然。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    NSLog(@"mod:numberOfSectionsInTableView");
    NSLog(@"[preferences count]=%d",[preferences count]);
    return  [preferences count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.choices count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
    NSLog(@"cellForRowAtIndexPath: DISPLAY TEST");  
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        NSLog(@"Text is: %@", [choices objectAtIndex:indexpath.row]);                   
        NSLog(@"CHOICE AT INDEX PATH IS: %@",[choices objectAtIndex:indexpath.row %[choices count]]);
        cell.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor blackColor];


        // Thumbs up button.
        //UIButton *thumbsUp = [[UIButton alloc]init];  
        thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];                        
        [thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
        [thumbsUp addTarget:self action:@selector(thumbUp_ButtonClicked:event:)
            forControlEvents:UIControlEventTouchUpInside];     
        [thumbsUp setTitle:@"" forState:UIControlStateNormal];
        thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
        [thumbsUp setBackgroundImage:[UIImage imageNamed:@"thumbsup_not_selected.png"]
     forState:UIControlStateNormal];

        //NSLog(@"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
        [cell.contentView addSubview:thumbsUp];

        // Thumbs down button

        thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
        [thumbsDown addTarget:self  action:@selector(thumbDown_ButtonClicked:event:)
            forControlEvents:UIControlEventTouchUpInside];
        [thumbsDown setTitle:@"" forState:UIControlStateNormal];
        thumbsDown.frame = CGRectMake(200, 20, 20, 15);

        [thumbsDown setTag:indexpath.row+120];
        [cell.contentView addSubview:thumbsDown];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];


        [thumbsDown setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]
            forState:UIControlStateNormal];
    }

    NSLog(@"------------> TAG TEST %d",thumbsUp.tag);
    cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];


    NSLog(@"HELLO FOR TESTING");
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(15, 10, 300, 25);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textAlignment = UITextAlignmentLeft;
    label.text = sectionTitle;


    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
    [view autorelease];
    [view addSubview:label];
    //[view addSubview:segmentedControl];
    view.backgroundColor = [UIColor grayColor];
    return view;
}

//Thumbs Up Button Action

- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
{
    NSLog(@"Thumbs Up Check!");


    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];


    NSIndexPath *indexPath = [myTable indexPathForCell:cell];
    NSLog(@"indexpath =%d",indexPath.row);
    //[button setTag:indexPath.row+(indexPath.section * 50)];

    int cTag = [sender tag];
    NSLog(@"------>TAG : %d", cTag);
    NSLog(@"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
    if(cTag == (indexPath.row+(indexPath.section * 50)))
    {
        NSLog(@"BUTTON COUNT:");
        [button setBackgroundImage:[UIImage imageNamed:@"thumbsup_selected.png"]
            forState:UIControlStateNormal];
    }


    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    //int row = button.tag;

    NSLog(@"SECTION IS:%d",section);
    NSLog(@"ROW IS: %d",row);


    NSArray *array = cell.contentView.subviews;
    NSLog(@"NUMBER OF OBJECTS: %d",[array count]);
    UIButton *test = (UIButton *)[array objectAtIndex:2];

    [test setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]forState:UIControlStateNormal];

}

由于按钮标签问题,当我更改一个按钮的图像时,几个按钮正在更改。如果有人能找到解决方案,那将会很有帮助......标签是为我们可以查看的部分中的按钮设置的。

I have two buttons inside sectioned tableview cell thumbs up and thumbs down. Initially image of both button is not selected. When I select thumbs up button its image become thumbs up selected and other one become thumbsdown not selected and vice versa.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    NSLog(@"mod:numberOfSectionsInTableView");
    NSLog(@"[preferences count]=%d",[preferences count]);
    return  [preferences count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.choices count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
    NSLog(@"cellForRowAtIndexPath: DISPLAY TEST");  
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        NSLog(@"Text is: %@", [choices objectAtIndex:indexpath.row]);                   
        NSLog(@"CHOICE AT INDEX PATH IS: %@",[choices objectAtIndex:indexpath.row %[choices count]]);
        cell.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor blackColor];


        // Thumbs up button.
        //UIButton *thumbsUp = [[UIButton alloc]init];  
        thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];                        
        [thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
        [thumbsUp addTarget:self action:@selector(thumbUp_ButtonClicked:event:)
            forControlEvents:UIControlEventTouchUpInside];     
        [thumbsUp setTitle:@"" forState:UIControlStateNormal];
        thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
        [thumbsUp setBackgroundImage:[UIImage imageNamed:@"thumbsup_not_selected.png"]
     forState:UIControlStateNormal];

        //NSLog(@"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
        [cell.contentView addSubview:thumbsUp];

        // Thumbs down button

        thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
        [thumbsDown addTarget:self  action:@selector(thumbDown_ButtonClicked:event:)
            forControlEvents:UIControlEventTouchUpInside];
        [thumbsDown setTitle:@"" forState:UIControlStateNormal];
        thumbsDown.frame = CGRectMake(200, 20, 20, 15);

        [thumbsDown setTag:indexpath.row+120];
        [cell.contentView addSubview:thumbsDown];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];


        [thumbsDown setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]
            forState:UIControlStateNormal];
    }

    NSLog(@"------------> TAG TEST %d",thumbsUp.tag);
    cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];


    NSLog(@"HELLO FOR TESTING");
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(15, 10, 300, 25);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textAlignment = UITextAlignmentLeft;
    label.text = sectionTitle;


    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
    [view autorelease];
    [view addSubview:label];
    //[view addSubview:segmentedControl];
    view.backgroundColor = [UIColor grayColor];
    return view;
}

//Thumbs Up Button Action

- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
{
    NSLog(@"Thumbs Up Check!");


    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];


    NSIndexPath *indexPath = [myTable indexPathForCell:cell];
    NSLog(@"indexpath =%d",indexPath.row);
    //[button setTag:indexPath.row+(indexPath.section * 50)];

    int cTag = [sender tag];
    NSLog(@"------>TAG : %d", cTag);
    NSLog(@"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
    if(cTag == (indexPath.row+(indexPath.section * 50)))
    {
        NSLog(@"BUTTON COUNT:");
        [button setBackgroundImage:[UIImage imageNamed:@"thumbsup_selected.png"]
            forState:UIControlStateNormal];
    }


    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    //int row = button.tag;

    NSLog(@"SECTION IS:%d",section);
    NSLog(@"ROW IS: %d",row);


    NSArray *array = cell.contentView.subviews;
    NSLog(@"NUMBER OF OBJECTS: %d",[array count]);
    UIButton *test = (UIButton *)[array objectAtIndex:2];

    [test setBackgroundImage:[UIImage imageNamed:@"thumbsdown_not_selected.png"]forState:UIControlStateNormal];

}

Due to issue with tag of button while I change image of one button several buttons are changing. If any one can please find a solution it will be helpful.... tag is setting for buttons in sections which we can view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江城子 2024-12-20 02:33:58

原因是回收/重用机制的使用不当(75%的关于 UITableView 的问题......)

去阅读 Apple 文档中的 Table View 编程指南(并在 SO 和网络上搜索与 tableview 和重用机制相关的任何问题)

The reason is the bad use of the recycling/reuse mechanism (as with 75% of questions about UITableView…)

Go read the Table View Programming Guide in Apple's doc (and search SO and the web for any question related to tableview and the reuse mechanism)

幻梦 2024-12-20 02:33:58

通过在 if(cell == nil) 外部和内部创建按钮纠正了该问题。还创建了一个可变字典来保持按钮的当前状态......

Corrected the issue by creating buttons outside and inside if(cell == nil). Also created a mutable dictionary to keep the current state of the button.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文