How to put a view with LoadMore Button in UITableView after the cell
How many rows are you telling the tableview to have?
How are you controlling which version of the prototype cell is displaying?
numberOfRowsInTableView(tableView: UITableView) -> Int {
return dataSources.count + 1
}
func tableView(tableView: UITableView, CellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexpath.row > dataSources.count {
return LoadMoreCell()
}
return NormalCell()
}
the method signature might be a little different there but the concept is the same
Add a button load more below uitableview
Here is the simplest way to add a button/CustomView
at the bottom of the tableView
.
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: self.tableView.frame.width, height: 40)))
button.setTitle("Load more", for: .normal)
button.backgroundColor = .lightGray
button.addTarget(self, action: #selector(moreButtonClicked(_:)), for: .touchUpInside)
self.tableView.tableFooterView = button
}
@objc func moreButtonClicked(_ sender: UIButton) {
print("More button clicked, fetch more data!")
}
Load more options with UITableView
Create Outlets in Interface Builder for tableview and make two dynamic prototype cells give them identifiers make one cell with a button(your load more cell button)
Then create action with that button that will contain the logic to load more cells!!!
now see the snipet below for reference...
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tblDemo: UITableView!
var arForCells:NSMutableArray = NSMutableArray()
let simpleCellID = "SimpleCell"
let loadMoreCell = "LoadMoreCell"
override func viewDidLoad() {
super.viewDidLoad()
tblDemo.delegate = self
tblDemo.dataSource = self
arForCells = NSMutableArray(objects: "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arForCells.count + 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.row == arForCells.count){
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(loadMoreCell, forIndexPath: indexPath)
return cell
}else {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(simpleCellID, forIndexPath: indexPath)
let lblCounter = cell.viewWithTag(111) as! UILabel
lblCounter.text = arForCells.objectAtIndex(indexPath.row) as! String
return cell
}
}
@IBAction func loadMoreCells(sender: AnyObject) {
let newAr:NSArray = NSArray(objects: "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
arForCells.addObjectsFromArray(newAr as [AnyObject])
tblDemo.reloadData()
}}
as I have checked, It should give you the desired results.
You could also do the same in TableViewFooter.
making a load more button in uitableviewcell?
to show an extra row in the tableview, in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return dataRows+1;
}
In
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//after setting tableviewcell
if(indexPath.row==dataRows){
cell.textLabel.text=@"Load More Rows";
}
}
In
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==dataRows){
//there you can write code to get next rows
}
}
You need to update the numberOfRows variable according to the displayed rows.
Edit: After fetching the extra entries, you can add them to the existing entries array using the following method. Your original array should be a NSMutableArray
to use this method.
[originalEntriesArray addObjectsFromArray:extraEntriesArray];
Expand UILabel inside UITableView with more button like Instagram
Here is an example: https://github.com/DonMag/DynamicCellHeight
Table B is one way of accomplishing "More/Less" (Table A was for another layout I played around with). It uses the [tableView beginUpdates]; tableView endUpdates];
method of triggering the table re-layout.
The key is getting all your constraints set up correctly, so the Auto-Layout engine does what you expect.
The example is in Swift, but should be really easily translated back to Obj-C (I think I did it in Obj-C first).
Edit: some additional notes...
This is using a pretty standard method of dynamic-height table view cells. The vertical spacing constraints between elements effectively "pushes out" the bounds of the cell. The tap here toggles the numberOfLines
property of the label between 2 and 0 (zero meaning as many lines as necessary). Sequential calls to beginUpdates
/ endUpdates
on the table view tells the auto-layout engine to recalculate the row heights without needing to reload the data.
For this example, I did use a little "trickery" to get the smooth expand/collapse effect... The multiline label you see here is contained in a UIView
(with clipsToBounds
true). There is a second, duplicate multiline label (alpha 0 so it's not visible) that is controlling the height. I found that changing the numberOfLines
on the visible label sort of "snapped" to 2 lines, and then the size change animation took place... resulting in the text "jumping around."
Just for the heck of it, I added the "not so good" version to my GitHub repo for comparison's sake.
How to add a button to the end of UIScrollView to load more?
So let's first think about how we initialize our table view. We use
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
to return the number of cells we want in the table, so let's say we use something like
return searchResultsArray.count
but if we want a "Load More Results" button, then let's add 1 more cell
return searchResultsArray.count + 1
Okay great, now how do we add the button in this new cell? We want to use
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
and we'll say
if indexPath.row == searchResultsArray.count
(remember that an array's count value is 1 greater than the index of the last element in the array, so this if statement is basically saying, "if this cell is the load more cell")
then we will create our button in this cell, and we can link a function to the button (using a selector) which will add results to the searchResultsArray, and then reload the table (using tableView.reloadData())
This is how you would approach this if it were a (vertical scrolling) table view, but if you want to recreate the horizontal scrolling interface in zillow, then you need to use a collection view, which is a bit complicated if you're new to iOS development, but still use the basic concept I described above. Here's a great video to help you with that: https://www.youtube.com/watch?v=Ko9oNhlTwH0
load more for UITableView in swift
To answer your question made a small test! One possible solution to this problem is to make another array and add it to the data that you want to display in the table and load the data to the same that you want to load! Loading data will willDisplayCell forRowAtIndexPath
var allObjectArray: NSMutableArray = []
var elements: NSMutableArray = []
var currentPage = 0
var nextpage = 0
override func viewDidLoad() {
super.viewDidLoad()
for var i = 0; i <= 500; i++ {
allObjectArray.addObject(i)
}
elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(0, 20)))
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
println(indexPath.row)
nextpage = elements.count - 5
if indexPath.row == nextpage {
currentPage++
nextpage = elements.count - 5
elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, 20)))
tableView.reloadData()
}
}
test project
https://drive.google.com/file/d/0B-CqajDlBoGHSE1OcjFoZWJxUTg/view?usp=sharing
Related Topics
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property
Uisearchcontroller Hiding Status Bar on iOS 11 Swift 4
How to Remove The Fading Animation on .Ondelete Swiftui
Nssavepannel - How to Restrict User to Only Save One One Set Directory
Nsnumber/Nsdecimalnumber Bizarre Behavior
How to Resolve Rctpromiseresolveblock After Bftask
Customize Mglpolyline Using Mapbox
Touch Sprite, Make It Jump Up Then Fall Down Again(Repeat as Many Times as Spritenode Is Tapped.)
Mkpolyline Broken When Using Type Satelliteflyover
How to Constrain Second Nsviewcontroller Minimum Size in Os X App
Can't Update Label from Other Class in Swift
How to Download Datas from Multiple Url in Concurrency Mode
Swift: How to Continuously Send an Action from a Nstextfield
Pfuser That Created Object Cannot Delete Object Despite Acl Writing Access
How to Observe Torchlevel in Swift
What's a Placeholder? Why Do We Need to Define Placeholder Data Type for Generics