Fatal error: Index out of range when delete cell from tableview rxswiftHow to use BehaviorRelay as an alternate to Variable in RxSwift?The first UITableViewCell's accessary arrow is not showingafter adding header cells don't appearHow to detect tableView cell touched or clicked in swiftExpand and Collapse tableview cellsUpdate or reload UITableView after completion of delete action on detail viewTableView not displaying text with JSON data from API callI am trying to append a message for my tableview but it is not appending.please help meSwift Error - Use of undeclared type 'cell' - Collection ViewSwift vertical UICollectionView inside UITableViewHow to search in Firebase Database Swift

Why alcohol had been selected as fuel for the first American space rockets?

Did Voldemort kill his father before finding out about Horcruxes?

How to say no to more work as a PhD student so I can graduate

Why isn't aluminium involved in biological processes?

Manually select/unselect lines before forwarding to stdout

What are the arguments for California’s nonpartisan blanket (jungle) primaries?

Why are road bikes (not time trial bikes) used in many triathlons?

Intel 8080-based home computers

How can I leave a car for someone if we can't meet in person?

Why does "git status" show I'm on the master branch and "git branch" does not?

Is this artwork (used in a video game) real?

What details should I consider before agreeing for part of my salary to be 'retained' by employer?

Do I need a 50/60Hz notch filter for battery powered devices?

What exactly is a Hadouken?

Why doesn't philosophy have higher standards for its arguments?

What happens if there is no space for entry stamp in the passport for US visa?

What is the meaning of [[:space:]] in bash?

How do I query for system views in a SQL Server database?

Interviewing with an unmentioned 9 months of sick leave taken during a job

Is the Gritty Realism variant incompatible with dungeon-based adventures?

FPGA CPU's, how to find the max speed?

Unix chat server making communication between terminals possible

What made Windows ME so crash-prone?

Construct, in some manner, a four-dimensional "RegionPlot"



Fatal error: Index out of range when delete cell from tableview rxswift


How to use BehaviorRelay as an alternate to Variable in RxSwift?The first UITableViewCell's accessary arrow is not showingafter adding header cells don't appearHow to detect tableView cell touched or clicked in swiftExpand and Collapse tableview cellsUpdate or reload UITableView after completion of delete action on detail viewTableView not displaying text with JSON data from API callI am trying to append a message for my tableview but it is not appending.please help meSwift Error - Use of undeclared type 'cell' - Collection ViewSwift vertical UICollectionView inside UITableViewHow to search in Firebase Database Swift






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have simple tableview. . When I want to delete cell from table view, I get that error.



View controller code:



class FoodCategoryDetailTableViewController: UITableViewController {
var foodCategoryDetailViewModel: FoodCategoryDetailTableViewViewModelType?

private let disposeBag = DisposeBag()

override func viewDidLoad()
super.viewDidLoad()

guard let foodCategoryDetailViewModel = foodCategoryDetailViewModel else return

tableView.delegate = nil
tableView.dataSource = nil

foodCategoryDetailViewModel.foodsInSelectedCategory
.bind(to: tableView.rx.items(cellIdentifier: FoodCategoryDetailTableViewCell.cellIdentifier, cellType: FoodCategoryDetailTableViewCell.self))
row, food, cell in
cell.foodCategoryDetailCellViewModel = foodCategoryDetailViewModel.cellViewModel(forRow: row)
.disposed(by: disposeBag)

tableView.rx.itemDeleted.subscribe( [unowned self] indexPath in
self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!)
self.tableView.reloadData()
).disposed(by: disposeBag)



View Model code:



class FoodCategoryDetailTableViewViewModel: FoodCategoryDetailTableViewViewModelType 
var foodsInSelectedCategory: BehaviorRelay<[Food]>
private var foodCategoryId: Int16

func cellViewModel(forRow row: Int) -> FoodTableViewCellViewModelType?
if let food = getFood(atRow: row)
return FoodTableViewCellViewModel(foodModel: food)

return nil


func removeFoodFromApplication(atRow row: Int)
if let food = getFood(atRow: row)
var foods = foodsInSelectedCategory.value
foods.remove(at: row)
self.foodsInSelectedCategory = BehaviorRelay(value: foods)
CoreDataHelper.sharedInstance.removeFoodFromApplication(foodName: food.name!)



private func getFood(atRow row: Int) -> Food?
let food = foodsInSelectedCategory.value[row]

return food


init(foodCategoryId: Int16)
self.foodCategoryId = foodCategoryId
self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId))




I am using Core Data. I get error in function getFood(). It error exist because row in view controller have old count of items in tableview. It is not updating with new count of items (foods) after delete cell.










share|improve this question






















  • Use numberOfRowsInSection = foods.count after all... or better put a tag on your cells, where tag = index of foodsInSelectedCategory ..this way even if you delete a row, the index shall stay there as reference

    – user3344236
    Mar 26 at 8:16












  • numberOfRowsInSection is not working =(

    – Tkas
    Mar 26 at 8:28






  • 1





    are u using RxDataSource? than self.tableView.reloadData() isn't it redundant ?? once u call self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!) table should be reloaded automotically

    – Sandeep Bhandari
    Mar 26 at 8:28






  • 1





    @tkas: You are setting tableView.dataSource = nil how do you expect number of sections and number of rows to get called if u r not using RxDataSource???

    – Sandeep Bhandari
    Mar 26 at 8:31






  • 1





    @Tkas: I don't think RxDataSource will help you. I think you have multiple problems here but the sample code doesn't compile so I can't tell for sure. On problem you absolutely have is this line self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId)). You should never replace a BehaviorRelay. It will break all of your existing subscriptions.

    – Daniel T.
    Mar 26 at 11:02


















1















I have simple tableview. . When I want to delete cell from table view, I get that error.



View controller code:



class FoodCategoryDetailTableViewController: UITableViewController {
var foodCategoryDetailViewModel: FoodCategoryDetailTableViewViewModelType?

private let disposeBag = DisposeBag()

override func viewDidLoad()
super.viewDidLoad()

guard let foodCategoryDetailViewModel = foodCategoryDetailViewModel else return

tableView.delegate = nil
tableView.dataSource = nil

foodCategoryDetailViewModel.foodsInSelectedCategory
.bind(to: tableView.rx.items(cellIdentifier: FoodCategoryDetailTableViewCell.cellIdentifier, cellType: FoodCategoryDetailTableViewCell.self))
row, food, cell in
cell.foodCategoryDetailCellViewModel = foodCategoryDetailViewModel.cellViewModel(forRow: row)
.disposed(by: disposeBag)

tableView.rx.itemDeleted.subscribe( [unowned self] indexPath in
self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!)
self.tableView.reloadData()
).disposed(by: disposeBag)



View Model code:



class FoodCategoryDetailTableViewViewModel: FoodCategoryDetailTableViewViewModelType 
var foodsInSelectedCategory: BehaviorRelay<[Food]>
private var foodCategoryId: Int16

func cellViewModel(forRow row: Int) -> FoodTableViewCellViewModelType?
if let food = getFood(atRow: row)
return FoodTableViewCellViewModel(foodModel: food)

return nil


func removeFoodFromApplication(atRow row: Int)
if let food = getFood(atRow: row)
var foods = foodsInSelectedCategory.value
foods.remove(at: row)
self.foodsInSelectedCategory = BehaviorRelay(value: foods)
CoreDataHelper.sharedInstance.removeFoodFromApplication(foodName: food.name!)



private func getFood(atRow row: Int) -> Food?
let food = foodsInSelectedCategory.value[row]

return food


init(foodCategoryId: Int16)
self.foodCategoryId = foodCategoryId
self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId))




I am using Core Data. I get error in function getFood(). It error exist because row in view controller have old count of items in tableview. It is not updating with new count of items (foods) after delete cell.










share|improve this question






















  • Use numberOfRowsInSection = foods.count after all... or better put a tag on your cells, where tag = index of foodsInSelectedCategory ..this way even if you delete a row, the index shall stay there as reference

    – user3344236
    Mar 26 at 8:16












  • numberOfRowsInSection is not working =(

    – Tkas
    Mar 26 at 8:28






  • 1





    are u using RxDataSource? than self.tableView.reloadData() isn't it redundant ?? once u call self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!) table should be reloaded automotically

    – Sandeep Bhandari
    Mar 26 at 8:28






  • 1





    @tkas: You are setting tableView.dataSource = nil how do you expect number of sections and number of rows to get called if u r not using RxDataSource???

    – Sandeep Bhandari
    Mar 26 at 8:31






  • 1





    @Tkas: I don't think RxDataSource will help you. I think you have multiple problems here but the sample code doesn't compile so I can't tell for sure. On problem you absolutely have is this line self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId)). You should never replace a BehaviorRelay. It will break all of your existing subscriptions.

    – Daniel T.
    Mar 26 at 11:02














1












1








1








I have simple tableview. . When I want to delete cell from table view, I get that error.



View controller code:



class FoodCategoryDetailTableViewController: UITableViewController {
var foodCategoryDetailViewModel: FoodCategoryDetailTableViewViewModelType?

private let disposeBag = DisposeBag()

override func viewDidLoad()
super.viewDidLoad()

guard let foodCategoryDetailViewModel = foodCategoryDetailViewModel else return

tableView.delegate = nil
tableView.dataSource = nil

foodCategoryDetailViewModel.foodsInSelectedCategory
.bind(to: tableView.rx.items(cellIdentifier: FoodCategoryDetailTableViewCell.cellIdentifier, cellType: FoodCategoryDetailTableViewCell.self))
row, food, cell in
cell.foodCategoryDetailCellViewModel = foodCategoryDetailViewModel.cellViewModel(forRow: row)
.disposed(by: disposeBag)

tableView.rx.itemDeleted.subscribe( [unowned self] indexPath in
self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!)
self.tableView.reloadData()
).disposed(by: disposeBag)



View Model code:



class FoodCategoryDetailTableViewViewModel: FoodCategoryDetailTableViewViewModelType 
var foodsInSelectedCategory: BehaviorRelay<[Food]>
private var foodCategoryId: Int16

func cellViewModel(forRow row: Int) -> FoodTableViewCellViewModelType?
if let food = getFood(atRow: row)
return FoodTableViewCellViewModel(foodModel: food)

return nil


func removeFoodFromApplication(atRow row: Int)
if let food = getFood(atRow: row)
var foods = foodsInSelectedCategory.value
foods.remove(at: row)
self.foodsInSelectedCategory = BehaviorRelay(value: foods)
CoreDataHelper.sharedInstance.removeFoodFromApplication(foodName: food.name!)



private func getFood(atRow row: Int) -> Food?
let food = foodsInSelectedCategory.value[row]

return food


init(foodCategoryId: Int16)
self.foodCategoryId = foodCategoryId
self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId))




I am using Core Data. I get error in function getFood(). It error exist because row in view controller have old count of items in tableview. It is not updating with new count of items (foods) after delete cell.










share|improve this question














I have simple tableview. . When I want to delete cell from table view, I get that error.



View controller code:



class FoodCategoryDetailTableViewController: UITableViewController {
var foodCategoryDetailViewModel: FoodCategoryDetailTableViewViewModelType?

private let disposeBag = DisposeBag()

override func viewDidLoad()
super.viewDidLoad()

guard let foodCategoryDetailViewModel = foodCategoryDetailViewModel else return

tableView.delegate = nil
tableView.dataSource = nil

foodCategoryDetailViewModel.foodsInSelectedCategory
.bind(to: tableView.rx.items(cellIdentifier: FoodCategoryDetailTableViewCell.cellIdentifier, cellType: FoodCategoryDetailTableViewCell.self))
row, food, cell in
cell.foodCategoryDetailCellViewModel = foodCategoryDetailViewModel.cellViewModel(forRow: row)
.disposed(by: disposeBag)

tableView.rx.itemDeleted.subscribe( [unowned self] indexPath in
self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!)
self.tableView.reloadData()
).disposed(by: disposeBag)



View Model code:



class FoodCategoryDetailTableViewViewModel: FoodCategoryDetailTableViewViewModelType 
var foodsInSelectedCategory: BehaviorRelay<[Food]>
private var foodCategoryId: Int16

func cellViewModel(forRow row: Int) -> FoodTableViewCellViewModelType?
if let food = getFood(atRow: row)
return FoodTableViewCellViewModel(foodModel: food)

return nil


func removeFoodFromApplication(atRow row: Int)
if let food = getFood(atRow: row)
var foods = foodsInSelectedCategory.value
foods.remove(at: row)
self.foodsInSelectedCategory = BehaviorRelay(value: foods)
CoreDataHelper.sharedInstance.removeFoodFromApplication(foodName: food.name!)



private func getFood(atRow row: Int) -> Food?
let food = foodsInSelectedCategory.value[row]

return food


init(foodCategoryId: Int16)
self.foodCategoryId = foodCategoryId
self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId))




I am using Core Data. I get error in function getFood(). It error exist because row in view controller have old count of items in tableview. It is not updating with new count of items (foods) after delete cell.







ios swift uitableview rx-swift






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 8:12









TkasTkas

758 bronze badges




758 bronze badges












  • Use numberOfRowsInSection = foods.count after all... or better put a tag on your cells, where tag = index of foodsInSelectedCategory ..this way even if you delete a row, the index shall stay there as reference

    – user3344236
    Mar 26 at 8:16












  • numberOfRowsInSection is not working =(

    – Tkas
    Mar 26 at 8:28






  • 1





    are u using RxDataSource? than self.tableView.reloadData() isn't it redundant ?? once u call self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!) table should be reloaded automotically

    – Sandeep Bhandari
    Mar 26 at 8:28






  • 1





    @tkas: You are setting tableView.dataSource = nil how do you expect number of sections and number of rows to get called if u r not using RxDataSource???

    – Sandeep Bhandari
    Mar 26 at 8:31






  • 1





    @Tkas: I don't think RxDataSource will help you. I think you have multiple problems here but the sample code doesn't compile so I can't tell for sure. On problem you absolutely have is this line self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId)). You should never replace a BehaviorRelay. It will break all of your existing subscriptions.

    – Daniel T.
    Mar 26 at 11:02


















  • Use numberOfRowsInSection = foods.count after all... or better put a tag on your cells, where tag = index of foodsInSelectedCategory ..this way even if you delete a row, the index shall stay there as reference

    – user3344236
    Mar 26 at 8:16












  • numberOfRowsInSection is not working =(

    – Tkas
    Mar 26 at 8:28






  • 1





    are u using RxDataSource? than self.tableView.reloadData() isn't it redundant ?? once u call self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!) table should be reloaded automotically

    – Sandeep Bhandari
    Mar 26 at 8:28






  • 1





    @tkas: You are setting tableView.dataSource = nil how do you expect number of sections and number of rows to get called if u r not using RxDataSource???

    – Sandeep Bhandari
    Mar 26 at 8:31






  • 1





    @Tkas: I don't think RxDataSource will help you. I think you have multiple problems here but the sample code doesn't compile so I can't tell for sure. On problem you absolutely have is this line self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId)). You should never replace a BehaviorRelay. It will break all of your existing subscriptions.

    – Daniel T.
    Mar 26 at 11:02

















Use numberOfRowsInSection = foods.count after all... or better put a tag on your cells, where tag = index of foodsInSelectedCategory ..this way even if you delete a row, the index shall stay there as reference

– user3344236
Mar 26 at 8:16






Use numberOfRowsInSection = foods.count after all... or better put a tag on your cells, where tag = index of foodsInSelectedCategory ..this way even if you delete a row, the index shall stay there as reference

– user3344236
Mar 26 at 8:16














numberOfRowsInSection is not working =(

– Tkas
Mar 26 at 8:28





numberOfRowsInSection is not working =(

– Tkas
Mar 26 at 8:28




1




1





are u using RxDataSource? than self.tableView.reloadData() isn't it redundant ?? once u call self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!) table should be reloaded automotically

– Sandeep Bhandari
Mar 26 at 8:28





are u using RxDataSource? than self.tableView.reloadData() isn't it redundant ?? once u call self.foodCategoryDetailViewModel?.removeFoodFromApplication(atRow: (indexPath.element?.row)!) table should be reloaded automotically

– Sandeep Bhandari
Mar 26 at 8:28




1




1





@tkas: You are setting tableView.dataSource = nil how do you expect number of sections and number of rows to get called if u r not using RxDataSource???

– Sandeep Bhandari
Mar 26 at 8:31





@tkas: You are setting tableView.dataSource = nil how do you expect number of sections and number of rows to get called if u r not using RxDataSource???

– Sandeep Bhandari
Mar 26 at 8:31




1




1





@Tkas: I don't think RxDataSource will help you. I think you have multiple problems here but the sample code doesn't compile so I can't tell for sure. On problem you absolutely have is this line self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId)). You should never replace a BehaviorRelay. It will break all of your existing subscriptions.

– Daniel T.
Mar 26 at 11:02






@Tkas: I don't think RxDataSource will help you. I think you have multiple problems here but the sample code doesn't compile so I can't tell for sure. On problem you absolutely have is this line self.foodsInSelectedCategory = BehaviorRelay(value: CoreDataHelper.sharedInstance.fetchFoodsFromSelectedCategory(foodCategoryId: self.foodCategoryId)). You should never replace a BehaviorRelay. It will break all of your existing subscriptions.

– Daniel T.
Mar 26 at 11:02













0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55352478%2ffatal-error-index-out-of-range-when-delete-cell-from-tableview-rxswift%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55352478%2ffatal-error-index-out-of-range-when-delete-cell-from-tableview-rxswift%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript