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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?