在uitableview中的单元格上添加单选按钮

时间:2012-12-24 11:35:45

标签: uitableview radio-button ibaction

我在uitableview的单元格中遇到自定义单选按钮的问题我正在使用下面的代码,我想为didselectrowatindexpath和单选按钮发件人操作设置相同的操作,以便我可以选择一个来自所有的单元格并取消选择表格视图中的所有其他单元格。在tableview我希望显示所有ipod音乐库歌曲,用户可以一次从中选择一首。


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell_Connections"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; cell.selectionStyle = UITableViewCellSelectionStyleGray; cell.detailTextLabel.textColor = [UIColor darkGrayColor]; [cell setAccessoryType: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]; } } } }

0 个答案:

没有答案