Vishal N
Vishal N

Reputation: 33

Reload all sections except first and second

I have Tablview which contains number of sections.

First Section - it's contain search textbox
second section - its's contain UIView
other sections - this section which i want to reload at the time of searching.

UPDATE
I am set countdic and tableview like below :-
1. set countdic

self.countDic.updateValue(1, forKey: "0")// this is for First section
self.countDic.updateValue(0, forKey: "1")//this is for second section
for i in 0..<arra1.count {
    self.countDic.updateValue(array.count, forKey: "\(i + 2)")//[index : arraylist.count]
}


2. Here is my tableview setup

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return countDic["\(section)"]!
 }

func numberOfSections(in tableView: UITableView) -> Int {
    return countDic.count
}


3. Here i am reload tableview
This is from shouldChangeCharactersIn of UITextfield

 let indexSet = IndexSet(integersIn: 2..<self.TblView.numberOfSections)
 self.TblView.reloadSections(indexSet, with: .bottom)

but it's crash the app with Error :

** reason: 'attempt to insert section 3 but there are only 3 sections after the update' **

Can anyone suggest how to done ?

Upvotes: 1

Views: 3653

Answers (4)

Shezad
Shezad

Reputation: 756

try this

let indexSet = IndexSet(integersIn: 1..<self.TblView.numberOfSections-1)

self.TblView.reloadSections(indexSet, with: .bottom)

Upvotes: 0

vadian
vadian

Reputation: 285082

This is Swift.

  • NS(Make)Range is inappropriate
    (The second parameter of NSMakeRange is the length not the end index by the way)
  • NSMutableIndexSet is inappropriate

Use the Swift capabilities:

let indexSet = IndexSet(integersIn: 2..<self.countDic.count)
self.TblView.reloadSections(indexSet, with: .bottom)

Upvotes: 5

Dharma
Dharma

Reputation: 3013

Use this function to reloads the specified sections

func reloadSections(_ sections: IndexSet, 
               with animation: UITableViewRowAnimation)

If you don't want reload first 2 sections of tableview then

tableView.reloadSections(IndexSet(2..<tableView.numberOfSection), with: .none)

Cheers!!!

Upvotes: 1

dahiya_boy
dahiya_boy

Reputation: 9503

Try this

In Obj-c

NSRange range = NSMakeRange(2, totalsection - 2);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];                                     
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone]

In Swift

var range = NSRange(location: 2, length: totalsection - 2)
var section = IndexSet(indexesIn: range)
tableView.reloadSections(section, with: .none)

Upvotes: 0

Related Questions