2

我在使用下面的代码时遇到了单元格上的自定义单选按钮问题,UItableViewView我想为didSelectRowAtIndexPath单选按钮发送者操作设置相同的操作,以便我可以从所有单元格中选择一个单元格并取消选择表格视图中的所有其他单元格。在 tableView 中,我想显示所有 iPod 音乐库歌曲,用户可以一次从中选择一首。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        静态 NSString *CellIdentifier = @"Cell_Connections";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        如果(单元格 == 零)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];
            [单元集附件类型:UITableViewCellAccessoryNone];
        }

cell.backgroundColor = [UIColor whiteColor]; cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16]; cell.textLabel.textColor = [UIColor blackColor]; cell.imageView.image = [UIImage imageNamed:@"cbimg_unselected.png"]; MPMediaItem *temp1 = [self.sectionedArtistsArray objectAtIndex:indexPath.row]; cell.textLabel.text = [temp1 valueForProperty:MPMediaItemPropertyTitle]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; CGRect frame = CGRectMake(0.0, 0.0, 44, 41); button.frame = frame; [button setImage:[UIImage imageNamed:@"cbimg_unselected.png"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"cbimg_selected.png"] forState:UIControlStateSelected]; [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; button.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview:button]; return cell; } - (void)checkButtonTapped:(id)sender event:(id)event { NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:self.tblVwMusic]; NSIndexPath *indexPath = [self.tblVwMusic indexPathForRowAtPoint: currentTouchPosition]; if (indexPath != nil) { //[self tableView: self.tblVwMusic accessoryButtonTappedForRowWithIndexPath: indexPath]; } UIButton *button = (UIButton*)sender; for (int section = 0, sectionCount = tblVwMusic.numberOfSections; section < sectionCount; ++section) { for (int row = 0, rowCount = [tblVwMusic numberOfRowsInSection:section]; row < rowCount; ++row) { UITableViewCell *cell = [tblVwMusic cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]]; [tblVwMusic deselectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:YES]; NSArray *btnsArray = [cell.contentView subviews]; for (UIButton *but in btnsArray) { if ([but isKindOfClass:[UIButton class]]) { [but setSelected:NO]; } } } } if (!button.selected) { button.selected = button.selected; }else { button.selected = !button.selected; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { for (int section = 0, sectionCount = tableView.numberOfSections; section < sectionCount; ++section) { for (int row = 0, rowCount = [tableView numberOfRowsInSection:section]; row < rowCount; ++row) { UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]]; [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:YES]; NSArray *btnsArray = [cell.contentView subviews]; for (UIButton *but in btnsArray) { if ([but isKindOfClass:[UIButton class]]) { [but setSelected:NO]; } } } } UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSArray *btnsArray = [cell.contentView subviews]; for (UIButton *but in btnsArray) { if ([but isKindOfClass:[UIButton class]]) { if (!but.selected) { [but setSelected:YES]; } else { [but setSelected:NO]; } } } }
4

0 回答 0