我正在从Web服务中检索"style"和"category"的值。它们都在UITableView
中分别显示。
多单元格选择选项也可用。我已经做了NSMutableArray
,我希望当用户从样式部分选择一个值时,它的值进入一个数组,同样,其他部分的值进入另一个数组。当他取消选择值时,它应该从数组中删除。下面的代码没有显示任何值,无论它是否被存储。它打印的只是(null)
.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if (indexPath.section==0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
selected[row] = NO;
NSLog(@"indexpath zero of un-select");
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
selected[row] = NO;
NSLog(@"indexpath else of un-select");
}
}
else
{
if (indexPath.section==0)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
selected[row] = YES;
[self.styleSelect objectAtIndex:indexPath.row];
NSLog(@"indexpath zero of select, %@",self.styleSelect[indexPath.row]); //this prints (null)
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
selected[row] = YES;
[self.categorySelect objectAtIndex:indexPath.row];
NSLog(@"indexpath else of select, %@",self.categorySelect[indexPath.row]); //this prints (null)
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
试试这个
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section==0)
{
[styleArray removeObject:indexPath];
}
else
{
[categoryArray removeObject:indexPath];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if (indexPath.section==0)
{
[styleArray addObject:indexPath];
}
else
{
[categoryArray addObject:indexPath];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
如果它打印"null",则仅表示您没有初始化数组,或者此类索引上有NSNull对象。此外,NSLog 之前的调用,即访问器调用是多余的。即应该有addObject而不是objectAtIndex,并且应该创建数组。
if (!self.styleSelect)
self.styleSelect = [NSMutableArray array];
[self.styleSelect addObject:@(indexPath.row)];
NSLog(@"indexpath zero of select, %@",self.styleSelect[indexPath.row]);