Insert one row at any index in TableView with custom Cell
Solution 1
From the comments i came to know you are very new to UITableView. So please go through some of the given tutorial. You will be able to add, remove edit cells in UITableView
UITableView tutorial
tutorial 2
Table view programming guide
For simple insertion do like this
Add button and its action to the view
-(IBAction)addNewRow:(UIButton *)sender
{
self.numberOfRows++; // update the data source
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Edit
I think you need some thing like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID1=@"CellOne";
NSString *cellID2=@"CellTwo";
UITableViewCell *cell = nil;
if (0 == indexPath.row) {
cell =[tableView dequeueReusableCellWithIdentifier:cellID1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID1];
}
cell.textLabel.text = @"New Cell";
}
else
{
cell =[tableView dequeueReusableCellWithIdentifier:cellID2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID2];
}
cell.imageView.image = [UIImage imageNamed:@"Test.jpg"];
}
return cell;
}
Dont forget increment the cell count ie now return 6 from numberOfRows.
Solution 2
you can insert rows into table from anywhere in the view controller
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation];
[tableView endUpdates];
give the proper indexPaths
and row animation.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 05, 2022Comments
-
Admin about 2 years
I'm newbie on iOS. I tried more time to search but the result is not my wish. The first, I have 5 rows with the custom cell. Cell contains only text and I want to add one row at any index with cell that contains one image. So how we can implement it ?