IOS - [UITableView reloadData] reloads but doesn't delete old cells?

This is strange, I know, but my [UITableView reloadData] does not delete old cells, for example: enter image description here

The damage you see occurred after I pressed the + button , returned and changed the values ​​again. plus button pushes navigation controller to another controller, and after I returned from the touch of a button back and changed the value, that's what I see. How is this possible ?? I used a custom view (subclassed from UIView) that I created as a UIStepper with UILabel. Here are the codes, the .m controller and the .h-.m files for the custom UIView.

controller.m

@interface ViewController ()
@property NSString *docsDir;
@property sqlite3 *DB;
@property NSArray *dirPaths;
@property NSString* databasePath;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property BOOL isCreatedBefore;
@property NSArray *theList;
@end

@implementation ViewController
@synthesize docsDir;
@synthesize DB;
@synthesize dirPaths;
@synthesize databasePath;
- (void)viewDidLoad
{
    [super viewDidLoad];
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: [NSString stringWithFormat:@"database.db"]]];

    [self createDatabase];
    self.theList = [self readAllEntries];
}

-(void) viewWillAppear:(BOOL)animated{
    self.theList = [self readAllEntries];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.theList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   SeriesObject * obj = [self.theList objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellocan"];
    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
    [nameLabel setText:obj.name];

    CounterView *seasonCounter = [[CounterView alloc] initWithX:220 WithY:28 WithName:obj.name withCount:obj.session withCustomTag:indexPath.row];
    seasonCounter.tag = 2;
    CounterView *episodeCounter = [[CounterView alloc] initWithX:268 WithY:28 WithName:obj.name withCount:obj.episode withCustomTag:indexPath.row];
    episodeCounter.tag = 4;

    [cell.contentView addSubview:seasonCounter];
    [cell.contentView addSubview:episodeCounter];

    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        SeriesObject *obj = [self.theList objectAtIndex:indexPath.row];
        [self deleteEntryWithID:obj.idd];

        self.theList = [self readAllEntries];
        [self.tableView reloadData];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 88;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

______and more about reading from database or deleting; not about the view. (this is a very simple app, as a free time hobby; so I didn't spend time to follow MVC)

Counterview.h

@interface CounterView : UIView
@property NSString* name;
@property NSInteger count;
@property UILabel *label;
@property NSInteger customTag;

- (id)initWithX:(CGFloat)xPoint WithY:(CGFloat)yPoint WithName:(NSString*)newName withCount:(NSInteger)newCount withCustomTag:(NSInteger)newTag;
@end

CounterView.m

@interface CounterView()

@property NSString *docsDir;
@property sqlite3 *DB;
@property NSArray *dirPaths;
@property NSString* databasePath;

@end


@implementation CounterView

@synthesize docsDir;
@synthesize DB;
@synthesize dirPaths;
@synthesize databasePath;

- (id)initWithX:(CGFloat)xPoint WithY:(CGFloat)yPoint WithName:(NSString*)newName withCount:(NSInteger)newCount withCustomTag:(NSInteger)newTag
{
    self = [super initWithFrame:CGRectMake(xPoint, yPoint, 24, 52)];
    if (self) {
        self.customTag = newTag;
        self.count = newCount;
        self.name = newName;

        UIButton *btnUp = [[UIButton alloc] initWithFrame:CGRectMake(3, 2, 18, 12)];
        [btnUp setImage:[UIImage imageNamed:@"top.png"] forState:UIControlStateNormal];
        [btnUp addTarget:self action:@selector(increaseValue) forControlEvents:UIControlEventTouchUpInside];
        UIButton *btnDown = [[UIButton alloc] initWithFrame:CGRectMake(3, 38, 18, 12)];
        [btnDown setImage:[UIImage imageNamed:@"bottom.png"] forState:UIControlStateNormal];
        [btnDown addTarget:self action:@selector(decreaseValue) forControlEvents:UIControlEventTouchUpInside];
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 24, 24)];
        [self.label setText:[NSString stringWithFormat:@"%ld", (long)self.count]];
        self.label.textAlignment = NSTextAlignmentCenter;

        [self addSubview:btnUp];
        [self addSubview:btnDown];
        [self addSubview:self.label];
    }
    return self;
}

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{

}


-(void) increaseValue{
    self.count++;
    [self.label setText:[NSString stringWithFormat:@"%ld", (long)self.count]];
}
-(void) decreaseValue{
    self.count--;
    [self.label setText:[NSString stringWithFormat:@"%ld", (long)self.count]];
}

____and some more database codes too..
+4
4

, cellForRowAtIndexpath: . .

if (cell != nil)
    {
        NSArray* subviews = [cell.contentView subviews];
        for (UIView* view in subviews)
        {
            [view removeFromSuperview];
        }
    }
+1

cellForRowAtIndexPath: subview . UITableView, "" . , . , . Google "UITableViewCell viewWithTag", .

+5
- (void)prepareForReuse
{
    //remove your subviews here 
    [self.subViewToRemove removeFromSuperView];
}
+2

, UITableViewCell, CounterView. subviews cellForRowAtIndexPath ( , ), , .

+1

Source: https://habr.com/ru/post/1530313/


All Articles