Vinathy nvr
Vinathy nvr

Reputation: 243

How to remove last object from NSMutableArray in ios without app crash

Here is my code

 (void)viewDidLoad

{
    descriptionArray = [[NSMutableArray alloc] initWithObjects:@"I'm not perfect,imake mistakes,i hurt people. But when i say sorry,i really mean it.",
                                                               @"Single",
                                                               @"[email protected]",
                                                               @"+91 9731244748",
                                                               @"jon.Korta",
                                                               @" 05 Common Friends",
                                                               @"Facebook", nil];

}

In UITableView Datasource Method

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
 static NSString *cellidentifier = @"CellID";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (cell == nil)

    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

     cell.descriptionLbl.text = [descriptionArray objectAtIndex:indexPath.row];

}

return cell;
}

CustomCell.h

@property (strong, nonatomic) IBOutlet UILabel *descriptionLbl;

I used CustomCell to display descriptionArray objects in UITableView cell.From the descriptionArray i want to remove "Facebook" object in the UITableView cell. Anybody can guide me.Thanks in advance

Upvotes: 1

Views: 5374

Answers (3)

MobileDev
MobileDev

Reputation: 1024

You can remove the last object by using [descriptionArray removeLastObject];.
After removing the last element you need to reload the table view. Dont forgot to reload the table view.

Upvotes: 1

seto nugroho
seto nugroho

Reputation: 1379

since you are using descriptionArray as your data source for cellForRow method, you need to make sure that numberOfRow is also returning your array count. So when you remove the last object in array and called table reload data it doesn't crash.

Upvotes: 0

Anshad Rasheed
Anshad Rasheed

Reputation: 2566

if you really want to remove the last object from your array just do any off the following

[descriptionArray removeObjectAtIndex:descriptionArray.count-1];

or

[descriptionArray removeLastObject];

and make sure that number of cells should be descriptionArray.count-1

Upvotes: 4

Related Questions