The last time I took a note on how to get a general tableview, I made some research. I went on to find that many apps have the "pull-down refresh" function, such as the app for everyone, there is also an app in the QQ space that feels cool. How can we use 320 to implement it? Let's take a look at my notes below.
First, create a project. Here I have created a project named freshtest, and then introduce 320 to OK to see if it can be built. If it can be built, it indicates that the project has been successfully introduced, after the success, we will start to write a tableview of 320. How can we write it in this article?
I have already written the code here, but I have already repeated it. After writing it, first check whether it can be run. It should be the following effect.
Well, after the operation is successful, we need to add such code in rootviewcontroller.
-(ID) createdelegate {
Return [[[tttableviewdragrefreshdelegate alloc] initwithcontroller: Self] autorelease];
}
Well, try running it and see if you can pull tableview down. Can you see a very familiar "pull-down and update? But don't be happy, because it only shows a view and does not do anything. If you don't believe it, drop it down or use the packet capture tool to capture the package and see what you have done. Actually, nothing is done!
Therefore, it is far from reaching the point of doing things. The above Code only declares a delegate, which has no practical effect yet, because we have to write datasource and model to implement details. Now we are just starting to implement the details.
Let's do nothing first, request a website, and then let it work first. For the moment, we will request a specific feed. First we will write a feed class. First we will create a feed class and then write the code as follows.
Header file.
# Import
<
Three20
/
Three1_h
>
@ Interface feed: nsobject {
Nsstring *
_ Text;
Nsstring
*
_ Source;
Nsstring
*
_ FTYPE;
Nsstring
*
_ IMG;
}
@ Property (nonatomic, copy) nsstring *
Text;
@ Property (nonatomic, copy) nsstring
*
Source;
@ Property (nonatomic, copy) nsstring
*
FTYPE;
@ Property (nonatomic, copy) nsstring
*
IMG;
@ End
.
# Import
"
Feed. h
"
@ Implementation feed
@ Synthesize text =
_ Text;
@ Synthesize Source
=
_ Source;
@ Synthesize FTYPE
=
_ FTYPE;
@ Synthesize img
=
_ IMG;
-
(
Void
) Dealloc {
Tt_release_safely (_ text );
Tt_release_safely (_ source );
Tt_release_safely (_ FTYPE );
Tt_release_safely (_ IMG );
[Super dealloc];
}
@ End
The above is just a simple feed class. After implementation, we need to write a model and create a model class. The implementation is as follows.
Header file.
# Import
<
Three20
/
Three1_h
>
@ Interface model: tturlrequestmodel {
Nsstring *
_ Searchquery;
Nsarray
*
_ Feeds;
}
@ Property (nonatomic, copy) nsstring *
Searchquery;
@ Property (nonatomic,
Readonly
) Nsarray
*
Feeds;
-
(ID) initwithsearchquery :( nsstring
*
) Searchquery;
@ End
Specific implementation.
# Import
"
Model. h
"
# Import
"
Feed. h
"
@ Implementation Model
@ Synthesize searchquery =
_ Searchquery;
@ Synthesize feeds
=
_ Feeds;
-
(ID) initwithsearchquery :( nsstring
*
) Searchquery {
If
(Self
=
[Super init]) {
Self. searchquery
=
Searchquery;
}
Return
Self;
}
-
(
Void
) Dealloc {
Tt_release_safely (_ searchquery );
Tt_release_safely (_ feeds );
[Super dealloc];
}
-
(
Void
) Load :( tturlrequestcachepolicy) cachepolicy more :( bool) more {
If
(
!
Self. isloading
&&
Ttisstringwithanytext (_ searchquery )){
Nsstring
*
URL
=
@"
Http://api.douban.com/people/2449296/miniblog/contacts/merged? Alt = JSON & Max-Results = 20
"
;
Tturlrequest
*
Request
=
[Tturlrequest
Requestwithurl: URL
Delegate
: Self];
Request. cachepolicy
=
Cachepolicy;
Request. cacheexpirationage
=
Tt_cache_expiration_age_never;
[Request send];
}
}
-
(
Void
) Requestdidfinishload :( tturlrequest
*
) Request {
}
@ End
Among them, the load method and requestdidfinishload are very important. Now, requestdidfinishload can be left empty without any implementation. Then, we will write another datasource as the tableview data source. OK. The code is still on.
Header file.
# Import
<
Three20
/
Three1_h
>
@ Class model;
//
Ttlistdatasource
@ Interface datasource: ttsectioneddatasource {
Model
*
_ Feed_model;
}
-
(ID) initwithsearchquery :( nsstring
*
) Searchquery;
@ End
Remember that the above Code is inherited from ttsectioneddatasource, and in the demo it is inherited from ttlistdatasource. Of course, both of them are
Yes, but there is a difference. If it inherits from ttlistdatasource, it is a normal tableview. Inherited from
Ttsectioneddatasource can have a category title.
Continue to implement.
# Import
"
Datasource. h
"
# Import
"
Model. h
"
# Import
"
Feed. h
"
@ Implementation datasource
-
(ID) initwithsearchquery :( nsstring
*
) Searchquery {
If
(Self
=
[Super init]) {
_ Feed_model
=
[[Model alloc] initwithsearchquery: searchquery];
}
Return
Self;
}
-
(
Void
) Dealloc {
Tt_release_safely (_ feed_model );
[Super dealloc];
}
-
(ID
<
Ttmodel
>
) Model {
Return
_ Feed_model;
}
-
(
Void
) Tableviewdidloadmodel :( uitableview
*
) Tableview {
}
-
(Nsstring
*
) Titleforloading :( bool) reloading {
If
(Reloading ){
Return
Nslocalizedstring (
@"
Updating feed...
"
,
@"
Feed updating text
"
);
}
Else
{
Return
Nslocalizedstring (
@"
Loading feed...
"
,
@"
Feed loading text
"
);
}
}
-
(Nsstring
*
) Titleforempty {
Return
Nslocalizedstring (
@"
No feed found.
"
,
@"
Feed no results
"
);
}
-
(Nsstring
*
) Subtitleforerror :( nserror
*
) Error {
Return
Nslocalizedstring (
@"
Sorry, there was an error loading the feed stream.
"
,
@""
);
}
@ End
After implementation, we can change our rootviewcontroller. There are not many changes, so we can change the M file. First add an import.
# Import "datasource. h"
Then, modify the createmodel method.
-(Void) createmodel {
Self. datasource = [[datasource alloc] initwithsearchquery: @ "Haha"] autorelease];
}
Run it. Is there a loading interface? Nothing.
It doesn't matter. Let's solve it now. In fact, it is very simple. Let's start with several codes to be implemented now. The main implementation is the requestdidfinishload method in the model and the tableviewdidloadmodel method in the datasource.
The model method is implemented as follows.
-(Void) requestdidfinishload :( tturlrequest *) Request {
Nsmutablearray * feeds = [[nsmutablearray alloc] initwithcapacity: 1];
Feed * f = [[feed alloc] init];
F. Text = @ "hi ";
[Feeds addobject: F];
_ Feeds = feeds;
[Super requestdidfinishload: request];
}
Then implement datasource.
-(Void) tableviewdidloadmodel :( uitableview *) tableview {
Nsmutablearray * items = [[nsmutablearray alloc] init];
For (feed * F in _ feed_model.feeds ){
[Items addobject: [tttabletextitem itemwithtext: f. Text];
}
Self. Items = items;
}
Then run it again. Well, is there a hi? Then let's pull down the page to see if it is obvious that there is a sense of load, and the update time has also changed?
In this case, the program will be OK. However, if you want to display it by category (like a contact person), you still need to change something. In the next article, I will talk about this and how to really request the internet.
The API on is then parse and displayed in the view. In fact, this is obvious, but it should be mentioned that there are some little magic points. :)
The sample code can be found here.
This article Reprinted from http://www.jguoer.com/blog/index.php/archives/1368