2013-07-19 28 views
0

我是iOS编程的新手,我在这里环顾了这里,并尝试了几种方法来解决这个问题,但似乎没有任何工作。我很确定这是因为我的一些愚蠢的错误:-)UISwitch在每个分区表行的附件查看

我有一个分节表,共约25行,分为2节。 每个cell.accessoryView都有一个开关,我试图在NSArray中存储开关状态,但是在NSArray中存储开关状态,但在任何情况下记录该索引处的数组内容奇怪地返回0。 (该阵列被实例化作为TableViewController的属性(nonatomic, readwrite),和合成的)

很显然,当我滚动表格上下,所有的开关返回默认OFF状态。

感谢您的帮助!

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
//countries dict and switch state array init.. 
self.countries = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"countries" ofType:@"plist"]]; 
NSNumber *b = [NSNumber numberWithBool:NO]; 
for (int i=0; i<210; i++) { 
    [statiSwitch addObject:b]; 
    NSLog(@"%d", [[statiSwitch objectAtIndex:i] integerValue]); 
} 

//tableview drawing.. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"CellWithSwitch";  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section]; 
NSString *country = [[self.countries valueForKey:continent] objectAtIndex:indexPath.row]; 

//Simple tag calculation... 
NSInteger tagOb = indexPath.section * 100 + indexPath.row; 

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
[mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged]; 
mySwitch.tag = tagOb; 
if ([statiSwitch count] > tagOb) { 
    [mySwitch setOn:[[statiSwitch objectAtIndex:tagOb] boolValue] animated:NO]; 
    } 

cell.textLabel.text = country;  
cell.accessoryView = mySwitch; 

return cell; 
} 

然后叫在开关状态变化的方法:

- (void) switchChange:(UISwitch *)sender { 
NSNumber *c = [NSNumber numberWithBool:sender.on]; 
[statiSwitch replaceObjectAtIndex:sender.tag withObject:c]; 

NSLog(@"switch tag is: %i and state is: %d", sender.tag, sender.on); 
NSLog(@"switch states array value: %d", [[statiSwitch objectAtIndex:sender.tag] integerValue]); 
} 
+0

尝试子类的UITableViewCell,并且整个细胞存储在数组中,而不是仅仅在开关值。然后,当tableView滚动时,将该单元格拉出数组而不是出队。 – AMayes

+0

因此,我应该建立一个包含所有20个单元对象(包括附件开关)的数组,然后使用' - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'方法从该数组中取出单元格? – Paolo83

+0

你应该继承UITableViewCell,并处理该类中的开关动作。这就像使用对象类来存储数据一样。 – AMayes

回答

0

我想你们了在向其添加对象之前省略以初始化statiSwitch。

在viewDidLoad中for循环之前,添加此行

statiSwitch = [NSMutableArray array]; 
+0

我将使用int数组..但你是对的@瑞克,我忘了在上面的代码中初始化数组:'statiSwitch'被声明为'viewController'的属性,综合等等,这就是我为什么认为没有必要分配并重新初始化。 – Paolo83

+0

不用担心,只要记住这一点。将会有很多情况下你仍然需要使用NSMutableArrays。如果你想学习iOS编程,你不能总是坚持C.干杯。 – Rick

0
  1. 设置自定义单元格类。
  2. 在故事板中创建一个原型单元格,并在其中放置一个开关。
  3. 将原型单元格设置为您的自定义类,并设置重用标识符。
  4. 将开关连接到您的自定义单元类中的IBOutlet。 (同时设立@protocol和委托,如果你希望你的细胞能够进行通信)
  5. 现在对于棘手/ hackish的部分:

在-viewDidLoad:

for (int i = 0; i < [self.cellDataArray count]; ++i) { 
      CustomCell *cell; 

    cell = [self.tableView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:nil]; 
    cell.delegate = self; 
    [self.cellsArray addObject:cell]; 
} 
  1. 在您的cellForRowAtIndexPath中,执行以下操作:

    cell = self.cellsArray [indexPath.row]; //或.item如果使用collectionView

  2. 由于交换机连接到每个单元,因此您需要一个CustomCell类中的方法,该方法将根据交换机执行某些操作,并且可能会向该委托类发送调用。

+0

非常感谢!我会尽力让你知道。 – Paolo83

+0

我试过这样的方式,对于单元格数组和其他单元格都没问题,但是我用'cell = [self.tableView dequeueReusableCellWithReuseIdentifier:@“Cell”forIndexPath:nil]编译器警告;'把它放在' -viewDidLoad:'方法。它告诉我'tableView'没有任何'dequeueReusableCellWithReuseIdentifier:'的标识符。我解决了它的解决方法,使用一个简单的int数组来存储开关值..不知道它不工作NSMutableArray NSObject存储值..请参阅我的答案的详细信息。 – Paolo83

+0

如果您已将@“Cell”更改为@“CellWithSwitch”,则不会出现该问题。 – AMayes

0

我用一个简单的int array存储交换机值,它似乎比工作取得了switchTag和switchState性能,这是我用之前做对象的NSMutableArray更好。

下面是代码:

- (void)viewDidLoad 
{ 
    //[here goes the code to retrieve my table data from a dictionary, getting sections and rows] 

    //int array iniazialization, with zeros to have switches set at OFF at beginning 

    for (int i = 0; i < 300; i++) { 
    switchStates[i] = 0; 
     } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cella"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

//Cell drawing.. 

NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section]; 
NSString *country = [[self.countries valueForKey:continent] objectAtIndex:indexPath.row]; 
cell.textLabel.text = country; 

//Calculate the tag to be assigned to the switches.. 

    switchTag = indexPath.section * 100 + indexPath.row 
    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
    [mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged]; 
    mySwitch.tag = switchTag; 
    mySwitch.on = switchStates[switchTag]; 

    cell.accessoryView = mySwitch; 

    return cell; 

} 

//switch change action.. 

- (void) switchChange:(UISwitch *)sender { 
    switchStates[sender.tag] = sender.on; 
} 

现在我有“刚”在这里为我即将到来的问题,存储的核心数据的交换价值..看你:-)

相关问题