簡體   English   中英

在uitableview中插入新行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM