objective c [ios] : หาความสูงรวมของ content ใน scroll view

นั่งงงกับปัญหานี้นานมาก 
เพราะใช้วิธีวนลูป subview ของ scrollview เพื่อหาความสูงรวม 
ดันไปเจอไอสอง subview ลับของมัน

โชคดีที่ subview ของ scroll view ไม่มี uiimage
เลยใช้ [view isKindOfClass:[UIImageView class]]  เช็คเอา[view isKindOfClass:[UIImageView class]]

http://wiresareobsolete.com/wordpress/2010/03/uiscrollview-has-a-secret/

 

 

Cache ใช้งานง่ายสำหรับ objective c (ios)

กำลังหัดเขียน ios app

ไปเจอส่วนที่จะใช้ cache เลยเอามาแปะไว้ใน blog

https://github.com/FTW/FTWCache

มีฟังก์ชั่นหลักๆอยู่แค่ 3-4 อัน

setObject สำหรับเก็บ NSData ลง cache โดยผูกไว้กับ key
objectForKey ก็ดึง object มาจาก cache โดยใช้ key
resetCache

 

ตัวอย่าง
สมมติเรามี url ของ image ที่เราจะแสดงอยู่แล้ว เราอาจใช้ MD5Hash ของ url นั้นเป็น key

NSString *key = [imageURL.absoluteString MD5Hash];
NSData *data = [FTWCache objectForKey:key]; //ลองเอา key ไปหาก่อน
if (data) { //ถ้ามีก็ใช้รูปจาก cache
UIImage *image = [UIImage imageWithData:data];
cell.imageView.image = image;
} else { //ถ้าไม่มีก็ไปดึงจาก web

NSData *data = [NSData dataWithContentsOfURL:imageURL]; //ไปดึง data มาจาก webservice
[FTWCache setObject:data forKey:key]; //เก็บลง cache
UIImage *image = [UIImage imageWithData:data];
[tableView cellForRowAtIndexPath:indexPath].imageView.image = image;
}

โค้ดตัวอย่างข้างบนเอามาจาก http://khanlou.com/2012/08/asynchronous-downloaded-images-with-caching/

objective c เรียก REST webservice

เอามาจาก http://rajneesh071.blogspot.com/2013/07/how-to-make-rest-api-call-in-objective-c.html

We can call using this.

NSString *twitterUrl = @”YourUrlString”;
NSString *resp = [self makeRestAPICall: twitterUrl];

and method is

-(NSString*) makeRestAPICall : (NSString*) reqURLStr
{
NSURLRequest *Request = [NSURLRequest requestWithURL:[NSURL URLWithString: reqURLStr]];
NSURLResponse *resp = nil;
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: Request returningResponse: &resp error: &error];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@”%@”,responseString);
return responseString;
}

วิธีใช้ NSUserDefaults

สำหรับคนที่กำลังหัดเขียนแอพ ios

มันมีวิธีเก็บข้อมูลในเครื่องอยู่ 4 วิธี คือ

  1. NSUserDefaults  ง่ายสุด เหมาะกับเก็บอะไรเล็กๆ สำหรับแอพที่ไม่ซับซ้อนมาก รู้สึกแรกว่าเริ่มเดิมทีมันเอาไว้เก็บ user setting  ไปๆมาๆใช้เก็บไปเรื่อย =_=
  2. property files (plist / XML)
  3. SQLLite  มันคือ relational database นั่นละ
  4. Core Data  framework ของ apple  ไม่รู้ ยังไม่ได้ดู

วันนี้จะมาโน้ตเรื่อง NSUserDefaults เฉยๆ เพราะกำลังลองใช้อยู่

วิธีเก็บค่า

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@”myfistname” forKey:@”firstName”];
[defaults synchronize];

วิธีดึงค่า

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:@”firstName”];
NSLog(@”%@”,firstName);

 

object ที่เก็บใน NSUserDefaults จะถูกแปลงให้เป็นพวก  immutable เพราะงั้นเวลาเก็บพวก NSMutableArray แล้วจะดึงค่าออกมาใช้ ต้องทำประมาณนี้

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *followingList = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@”followingList”]];

Overwriting wordpress get_the_excerpt to keep html tag

For anyone who’s trying to customize your wordpress theme and wonder how to get a chunk of your post contents, maybe first xx characters of your post, and display them on your homepage. WordPress comes with a function  called get_the_excerpt which will do exactly what you want BUT it will remove all your html tag.

For example, here’s how my post content looks like.

Screen Shot 2557-02-16 at 1.38.58 PM

You’ll see that there are new lines <br/> and paragraph <p> in my content. However, when I use get_the_excerpt in my homepage. It turns out like this.

Screen Shot 2557-02-16 at 1.38.44 PM

As you can see, all new lines and paragraph are removed and my content are displayed in simple text format.

So, how to prevent this? Here’s how.

Open you functions.php in your theme folder and add this function

Screen Shot 2557-02-16 at 1.46.27 PM

This is to create a new function for excerpting. The magic line is strip_tag. I put only <p> here but you can put whatever tag you want. For example, you can use strip_tag($text,'<p><img>’) to get your img as well.

But just adding this function won’t work because get_the_exceprt() is still wiring to the old function. So you’ll need to add two extra lines to your theme functions.php

Screen Shot 2557-02-16 at 1.48.09 PM

This is to tell wordpress that when you call get_the_exceprt. You are going to call your own improved_trim_exceprt, not the wp_ one.

This is how it looks like after fixing. You can see that new lines and paragraph are now interpreted correctly.

Screen Shot 2557-02-16 at 1.56.27 PM

One thing worth noting is that this will affect every get_the_excerpt in your code, and usually, there are more than one place. For example facebook sharing (og:description) might also use get exceprt as description when sharing to facebook and you may want to remove html tag before doing so (because it looks like crap with html tag). To work around this, you might want to create more than one custom excerpt function.

Below are example of my custom functions.

Screen Shot 2557-02-16 at 1.54.37 PM

btw. I’m starting my new site which is an example in this post. Feel free to visit.

[iOS] NSInternalInconsistencyException, reason: request for rect at invalid index path

This error means that the application cannot retrieve cell for the specified indexPath — I think there are many things that can cause this but for my case, I just forget to modify

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

in datasource to have it returns actual number of section in the table (It is 0 by default)

Note : datasource for the table is usually the ViewController itself

[Android] self study 1

ในที่สุดก็สามารถเอาชนะความขี้เกียจ ลากตัวเองมาลองทำมือถือได้ เฮ (จะทำไปได้สักกี่วันว้า)

อันที่จริงก็แอบศึกษา iOS มาได้สักพักแล้ว เพราะอยากเขียนมือถือกับเค้ามั่ง แต่พอเมื่อวานได้ลองแตะ android แล้วรู้สึกว่าน่าจะเรียนรู้ได้เร็วกว่าด้วยพื้นฐาน java (อันน้อยนิด) ที่มี ฉะนั้นเราจะพับ iOS เก็บไป แล้วหันมาซบอกเจ้าหุ่นเขียวแทน  🙂

เนื่องจากชีวิตมนุษย์ลูกจ้างมีเวลาว่างหลังเลิกงานแต่ละวันไม่มากนัก ไหนจะเล่นมือถือ เล่นเฟสบุคอีก บล็อกนี้จึงไม่ได้หวังว่าจะเป็น tutorial ให้ใคร แต่จะออกเป็นแนวบันทึกว่าวันนี้ได้ทำอะไรไปมากกว่า เพราะฉะนั้นอย่าไปคาดหวังอะไรกับมันมากละกัน

 

เริ่มจาก IDE ก่อน จากการค้นคว้าเป็นเวลาห้านาที เหมือนว่า google จะปล่อยตัว SDK แบบมี android developer tool (ADT) ติดมาให้ในตัวแล้ว สามารถโหลดได้ที่ http://developer.android.com/sdk/index.html

ตัว IDE ก็ based on eclipse เลย GUI หน้าตาเดียวกันเด๊ะ (เฮ)

สำหรับคำถามว่ามันต่างกับใช้ eclipse + android plugin ยังไง ผมก็อยากรู้เหมือนกัน ใครรู้มาบอกด้วย

ผมลองเซิสประมาณว่า  android adt bundle vs eclipse  แต่ไม่เจอคำตอบ

 

ชั่งมัน เอาไอนี่แหละ ขี้เกียจโหลด eclipse อีกตัว ถ้าไม่เวิร์คค่อยเปลี่ยน  //อันที่จริงผมมี eclipse ในเครื่องอยู่แล้ว แต่เอาไว้ทำ web app ไม่อยากลง android ให้มันตีกัน

 

หลังจากดาวโหลดเสร็จ
1. extract
2. เจอ folder eclipse
3. double click เปิดมันขึ้นมา เจอหน้าตา IDE
4. คลิก File > New > Android Application Project
5. ตั้งชื่อให้โปรเจค ส่วนทีเหลือปล่อย default หมด
6. คลิก Finish
7. เราก็จะได้หน้าตาประมาณนี้ !!!

Screen Shot 2556-07-02 at 9.58.09 PM

จะเห็นว่ามันมี activity_main.xml มาให้แล้ว ซึ่งมันแบ่งเป็นสอง tab อีกคือ Graphical Layout กับ ตัวที่เป็น xml

ไอตัว Graphical Layout จะมี text อยู่อันนึง(ชนิดของ text จะเรียกว่า TextView มั้ง) ซึ่งมีค่าว่า Hello world  ไอนี่มันจะไปแมพกับหน้า xml ตรง

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

นั่นเอง ทีนี้ไอ @string/hello_world มันคืออะไรเนี่ย?

ด้วยความชาญฉลาด อ่อนโลกและใสซื่อ ลองลบมันออกแล้วเปลี่ยนเป็น

         android:text="whaaaaat" />

ปรากฏว่าที่หน้า Graphical Layout มันเปลี่ยนตามด้วยเว้ยเฮ้ย 555 เรานี่มันอัจฉริยะจริงๆ

แต่ทำไมในหน้า xml มันขึ้นตัวเหลืองๆเนี่ย พอเอาเม้าไปชี้ก็โดนมันด่ามาว่า

[I18N] Hardcoded string “whaaaaat”, should use @string  resource

หรือแปลเป็นไทยได้ว่า “ที่เอ็ง hardcode string ไว้ว่า whaaaaat เนี่ย ชาวบ้านเค้าไม่ทำกันนะ หัดใช้ @string ซะ”

 

โดนคอมพิวเตอร์ด่า น้ำตาซึมไปสองวิ ก็กด ctrl + z ย้อนกลับไปเป็น  @string/hello_world  ก็ได้ฟะ

ทีนี้ด้วยไหวพริบปฏิภาณ อยากรู้ว่าไอ @string/hello_world มันคืออะไร ใช้ยังไง เราก็ทำการกด ctrl + h (search) แล้วพิมไปว่า hello_world

ไอ IDE มันก็จะไปหามาว่าไอ hello_world มันอยู่ซอกหลืบไหนของโปรเจค ซึ่งผลที่ได้ก็คือ

Screen Shot 2556-07-02 at 10.21.53 PM

มันอยู่ใน strings.xml นั่นเอง

ทีนี้พอคลิกไปก็จะมีสอง tab คล้ายๆ activity_main.xml เป็น tab Resources กับ tab xml  ผมชอบดู xml มากกว่า พอคลิกไปก็จะเจอ

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">FirstProject</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

ลองเปลี่ยนค่าไอ  Hello world!  เป็น Hello Android! ก็จะพบว่าค่าใน graphical layout มันก็เปลี่ยนตาม ไม่โดนคอมด่าด้วย เฮ

 

เป็นอันฟิน จบเอนทรี่แรก

//เอาจริงดิ ยังไม่ทันทำไรเลยนะ
//อืม ก็ขี้เกียจอะะะ  –___–
//ที่จริงนี่มันเบสิกมาก ทำไมเขียนยาวจัง