开发者

inserting a new row to a uitableview

i have a very simple table with only one row that has a uiswitch on it. basically, on开发者_开发知识库ce the user turns the switch on, i'd like to add another row below the first row. nothing seems to be working properly even after going through many threads here. in the best case, i can add another row but in a new section, which is not what i want.

here is some of my code:

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;

}

And the action associated with my switch:

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

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

    }

 }

I read somewhere that I need to use insertRowsAtIndexPaths: but i have no idea if i really need it in my case since I am not doing any editing to my cells - just adding one cell to the same section.

Is this the right way to do that? what am i doing wrong here?

Thanks!

EDIT:

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

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

}
    return count;
}


Did you change the value that numberOfRowsInSection returns, and then reloadData?


As I can not figure out in your piece of code, there may be the cause some where you are missing something READ this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜