How divide array and show in table view?

2019-09-13 19:09发布

问题:

I am developing a table view. To fill that table i have an array of 75 content. Now i want to show table view like as when first time shown table view then show only 10 row of array and one button whose title is show more. when click on button then show next 10 content of array in table view and in last of table view show two button previous and next. When click on next then again show next 10 content of array and when click on previous then show previous 10 content. Now problem is that what i will do with array so that i get this type look? How code for that in table view?

回答1:

You can go like below..

1.. Take a variable suppose n=11

2.. in cellForRowAtIndexPath write a code for only n-1 cells... and for nth cell create a button for first time that shows show more as you said

3.. Now in didSelectRowAtIndexPath write a code to identify that the selected cell has index == n?? if yes then call a method which will increment n with 10 and then again reload table.

4.. Go on incrementing and decrementing n as you want... but remember to reload data with nth row selection



回答2:

You can achieve this using tag property of button(any child of uiview inherits that) in following way.

You can divide array in an arrays of 10 element and store each of them in other array. Now to retrieve data you can set tag of the button. That is, for example, more button tag would be 1 initially. so when it is clicked you can retrieve 1st array from array of arrays and show it and set more button tag to 2. thus next time more button will cause fetching of 2nd element from big array. Same way previous will store respective tag and you can use that to fetch previous array.

Now above can be done without 2D array also. For that you can multiply moreButton.tab with 10 and display next 10 element of array.

Please put required validation check before updating tag property.

Hope it helps.