嗨,我想选择多个UITableViewCells并通过再次点击取消选择。
以下是didSelectRowAtIndexPath
:和cellForRowAtIndexPath
:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"serviceTypeSelectionCell";
ServiceTypeSelectionTableViewCell *serviceTypeCell = (ServiceTypeSelectionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (serviceTypeCell == nil)
{
serviceTypeCell = [[ServiceTypeSelectionTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//serviceDetailsCell.noOfQuantityTextField.tag = indexPath.row;
serviceTypeCell.serviceTypeNameLabel.text = [NSString stringWithFormat:@"%@",arrayOfServiceTypes[indexPath.row][@"sub_cat_name"]];
if([[arrayOfSelectedBoolValues objectAtIndex:indexPath.row] boolValue])
{
serviceTypeCell.serviceTypeImageView.image = [UIImage imageNamed:@"cancellation_reason_correct_icon"];
}
else {
serviceTypeCell.serviceTypeImageView.image = [UIImage imageNamed:@"cancellation_reason_unselect_icon"];
}
return serviceTypeCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (int i = 0; i < [arrayOfServiceTypes count]; i++)//Make all Rows To UnSelected
{
[arrayOfSelectedBoolValues replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
}
[arrayOfSelectedBoolValues addObjectsFromArray:sele:indexPath.row];
if(arrayOfSelectedBoolValues!=nil)
{
[arrayOfSelectedBoolValues replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
}
[arrayOfSelectedBoolValues replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
{
[self.tableView reloadData];
[self showOrHideAddButton];
}
我有一个单独选择和取消选择上述uitableview的现有代码。我只能通过禁用所有上述代码来获得多项选择,并将此部分保留在didSelectRowAtIndexPath
中:。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayOfSelectedBoolValues replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
[self.tableView reloadData];
[self showOrHideAddButton];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.doneButton.hidden = YES;
arrayOfSelectedBoolValues = [[NSMutableArray alloc]init];
for (int i = 0; i < [arrayOfServiceTypes count]; i++)
{
// [arrayOfexpOrCollBoolValues addObject:[NSNumber numberWithBool:NO]];
[arrayOfSelectedBoolValues addObject:[NSNumber numberWithBool:NO]];
}
[self sendServiceToGetServiceTypes];
}
所以请任何人根据我的代码发布结构化代码,以选择多个tableviewcells,并在将所选值发送到服务器之前再次点击取消选择任何单元格。
先谢谢。
答案 0 :(得分:0)
你需要这样做:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/// first run a loop for deselect all data
//Make all Rows To UnSelected
for (int i = 0; i < [arrayOfServiceTypes count]; i++)
{
[arrayOfSelectedBoolValues replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
}
Now which one you have selected you need to do like this:
[arrayOfSelectedBoolValues replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
[self.tableView reloadData];
}
我希望它能帮到你解决问题。
答案 1 :(得分:0)
您需要维护一个您选择的行数组,具体取决于您将选择一行还是取消选择行(如果该行已存在)。看起来像这样:
Swift 3代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
// Normal content
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Text="Twitter" />
<Button Grid.Column="1" Text="Facebook" />
</Grid>
</Grid>
对象代码:
var selectedArray : [IndexPath] = [IndexPath]()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let myCell = tableView.cellForRow(at: indexPath) as! TableCell
if(!selectedArray.contains(indexPath))
{
selectedArray.append(indexPath)
// Change cell image for selected if needed here.
}
else
{
// remove from array here if required
// Change cell image for un selected if needed here.
}
}
在.h文件中声明- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
.
.
.
if(![selectedArray containsObject:indexPath])
{
[selectedArray addObject:indexPath];
// Change cell image for selected if needed here.
}
else
{
[selectedArray removeObject:indexPath];
// remove from array here if required
// Change cell image for un selected if needed here.
}
.
.
.
}
(在界面中)并在selectedArray
中初始化,如此 - viewDidLoad
答案 2 :(得分:0)
尝试修改didSelectRowAtIndexPath,如
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_selectedIndexes == nil) {
self.selectedIndexes = [NSMutableArray array];
}
if ([_selectedIndexes containsObject:indexPath]) {
[_selectedIndexes removeObject:indexPath];
} else {
[_selectedIndexes addObject:indexPath];
}
[self.tableView reloadData];
[self showOrHideAddButton];
}
在cellForRowAtIndexPath
尝试修改代码以更改serviceTypeImageView
图片,如
if([_selectedIndexes containsObject:indexPath])
{
serviceTypeCell.serviceTypeImageView.image = [UIImage imageNamed:@"cancellation_reason_correct_icon"];
}
else {
serviceTypeCell.serviceTypeImageView.image = [UIImage imageNamed:@"cancellation_reason_unselect_icon"];
}
此处selectedIndexes
是一个可变数组,用于跟踪所选的indexPaths
@property(nonatomic, strong) NSMutableArray *selectedIndexes;