[英]inserting a new row to a uitableview
我有一个非常简单的表,只有一行,上面有一个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;
}
您是否更改了numberOfRowsInSection返回的值,然后重载了reloadData?
由于我无法在您的代码段中弄清楚,可能是由于某些原因而导致您缺少某些内容的原因,请阅读以下内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.