Aktualisieren Sie zuerst Ihr Datamodell (NSArray oder veränderbar). Zweitens, wenn Sie die Tabellenansicht aktualisieren möchten, fügen Sie einen Code hinzu [self.tableView reloadData];
oops, Ihr Code einige seltsame uitaleviewDataSource, Delegate Method.
auch, haben Sie einen Fehler.
Warum implementiert Sie NumberOfRowsInSection return 1? sehr eigenartig.
Ich habe unter dem Code geschätzt.
CASE 1. Nein hat DataModel.
- (IBAction)addCell:(id)sender
{
cellRowsCount++;
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cellRowsCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
return cell;
}
Ihre Codes, Datenmodell (NSArray oder wandelbar) muss nicht unbedingt, so dass ich einfach rowCount Variable hinzugefügt und Ihre rowsCount von tableviewCell synchronisiert hat.
Wenn Sie mit DataModel wollen, unter CASE2. Verweis auf PLZ.
CASE2. hat DataModel.
- (IBAction)addCell:(id)sender
{
textCount ++;
NSString *cellText = [NSString stringWithFormat:"blah %d", textCount];
[myArray addObject:cellText];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [myArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[myArray objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = (NSString *)[myArray objectAtIndex:indexPath.row];
return cell;
}
Was Sie getan haben, ist kein guter Weg, um mit 'UITableView' umzugehen! Sie haben 'UITableViewCell' an anderer Stelle erstellt, während es in' cellForRowAtIndexPath' empfohlen wird. Sie können tun, was Sie wollen, indem Sie einfach Ihre 'dataSource' ändern und dann die Tabelle neu laden. – tipycalFlow