我在BOOl
的每个部分使用TableView
值。第一部分有静态数据,第二部分有动态数据。我想在部分的每一行中给出bool值。但我无法这样做,因为第二部分有动态数据。能否请您建议我如何将每个单元格的bool值设置为静态或动态。
这是我的代码。
BOOL zero,one,dynamic
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
if (section == 0) {
return 2;
}if (section == 1) {
return [myData count];
}
return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
if (indexPath.row == 0 && indexPath.section == 0) {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView.hidden = NO;
zero = true;
}if (indexPath.row == 1 && indexPath.section == 0) {
one = true;
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView.hidden = NO;
}
if (indexPath.section == 2) {
cell = [tableView cellForRowAtIndexPath:indexPath];
dynamic = true;
}
}
在上面的代码中,我希望{2}中的每一行都有BOOL
值,请您建议我如何实现此功能?
答案 0 :(得分:1)
有一个简单的技巧,比如你想用每个动态数据添加BOOL值,所以只需添加一个 更多关键模型,并将默认值设置为0。
实施例: -
NSArray *responseArray = responseObject; //WEB SERVICE RESPONSE
// Add web service data to model
[responseArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
dataModel *data = [dataModel modelObjectWithDictionary:obj];
data.isBOOLValue=@(0)
[mydata addObject:objBillBorad];
}];
或者还有另一种方式
就像每次从Web服务向mydata
我认为这可能会解决您的问题。
如果不是这样,请告诉我,如果您有任何问题,我会尽快解决。
答案 1 :(得分:0)
这是table view
中.m
的完整实施。创建NSMutableArray
并在用户点按单元格时添加数据。这会在用户点按某个单元格时显示section
,row
和您的bool
值。看看这个。希望这对你有所帮助。
@interface ViewController ()
@end
@implementation ViewController
{
NSMutableArray *holdingArray;
Boolean _zero,_first,_dynamic;
}
- (void)viewDidLoad {
[super viewDidLoad];
holdingArray = [[NSMutableArray alloc] init];
_zero = _first = _dynamic = FALSE;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
return 2;
}
else
{
return 10; // you can use you dynamic data here.
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"SECTION 0";
}
else
{
return @"SECTION 1";
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *thecell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
if(indexPath.section == 0)
{
//do your static data binding here
}
else
{
// do your dynamic data binding here
}
return thecell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
//this means your static data section
if(indexPath.row == 0)
{
//this is your zeroth row in zeroth section
NSString *mysection = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
NSString *myrow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
_zero = TRUE;
NSString *addstring = [NSString stringWithFormat:@"Section :[%@] | Row :[%@] | Zero Value : [%@]",mysection,myrow,@"TRUE"];
[holdingArray addObject:addstring];
}
else
{
//this is your first row in zeroth section
NSString *mysection = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
NSString *myrow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
_first = TRUE;
NSString *addstring = [NSString stringWithFormat:@"Section :[%@] | Row :[%@] | First Value : [%@]",mysection,myrow,@"TRUE"];
[holdingArray addObject:addstring];
}
}
else
{
//this means your dynamic data section.
NSString *mysection = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
NSString *myrow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
_dynamic = TRUE;
NSString *addstring = [NSString stringWithFormat:@"Section :[%@] | Row :[%@] | Dynamic Value : [%@]",mysection,myrow,@"TRUE"];
[holdingArray addObject:addstring];
}
NSLog(@"selected cells details : %@", holdingArray);
}
@end