2011-08-03 5 views
0

私はそれにuiswitchを持つ行が1つしかない非常に単純なテーブルを持っています。基本的に、ユーザーがスイッチをオンにすると、私は最初の行の下に別の行を追加したいと思います。ここで多くのスレッドを通過した後も、何も正しく動作していないようです。最良の場合、私は別の行を追加することができますが、新しいセクションには、私が望むものではありません。私はinsertRowsAtIndexPathsを使用する必要があることをどこかで読んuitableviewに新しい行を挿入する

- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender { 
if (switch1.on) { 

    [listOfItems insertObject:@"LINE 2" atIndex:0]; 
    [tblSimpleTable reloadData]; 

    } 

} 

LoadDidView:

listOfItems = [[NSMutableArray alloc] init]; 
    listOfItems = [NSMutableArray arrayWithObjects:@"LINE 1" ,nil];  



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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; //try here diff styles 
} 

NSString *cellValue = [listOfItems objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellValue; 


switch1 = [[UISwitch alloc] initWithFrame:CGRectZero]; 
[switch1 addTarget:self action:@selector(toggleEnabledTextForSwitch1onSomeLabel:) forControlEvents:UIControlEventValueChanged]; 

[cell addSubview:switch1]; 
cell.accessoryView = switch1; 



return cell; 

} 

そして、私のスイッチに関連付けられたアクションここ

は、私のコードの一部ですが、同じセクションに1つのセルを追加するだけで、私は自分のセルに何も編集をしていないので、私の場合は本当に必要かどうか分かりません。

これは正しい方法ですか?私はここで間違って何をしていますか?

ありがとうございます!

EDIT:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    int count = [listOfItems count]; 
    if(self.editing) { 
    count++; 

} 
    return count; 
} 

答えて

0

あなたはnumberOfRowsInSectionが返す値を変更し、reloadDataましたか?

+0

は、私はそこに何をすべきかです: - (NSInteger)のtableView:(のUITableView *)のtableView numberOfRowsInSection:(NSInteger)セクション{ int count = [listOfItems count]; if(self.editing){ count ++; } \tリターンカウント; } – TommyG

+0

更新を追加しています... – TommyG

0

私は、コードのあなたの部分では把握できないので、あなたが何かREADに欠けているいくつかの原因がある可能性があり、ここでこの

関連する問題