我有一個非常簡單的表,只有一行上有一個uiswitch。基本上,一旦用戶打開開關,我想在第一行下面添加另一行。即使經過這裏的許多線程,似乎沒有任何工作正常。在最好的情況下,我可以添加另一行,但在一個新的部分,這不是我想要的。插入一個新行到一個可用視圖
這裏是我的一些代碼:
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;
}
而且與我的開關關聯的動作:
- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender {
if (switch1.on) {
[listOfItems insertObject:@"LINE 2" atIndex:0];
[tblSimpleTable reloadData];
}
}
我讀的地方,我需要使用insertRowsAtIndexPaths:但我不知道我是否真的需要它,因爲我沒有對我的單元格進行任何編輯 - 只是將一個單元格添加到同一部分。
這是否正確嗎?我在這裏做錯了什麼?
謝謝!
編輯:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int count = [listOfItems count];
if(self.editing) {
count++;
}
return count;
}
這裏是我做的有: - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger)section { int count = [listOfItems count]; if(self.editing)count ++; } \t return count; } – TommyG
在更新之上添加... – TommyG