看apple的官方示例代碼TheElements

TheElements:https://developer.apple.com/library/ios/samplecode/TheElements/Introduction/Intro.html.
是app的官方示例代碼.今天拜讀了之后,把一些收獲寫下來.

這個項目非常規(guī)范的使用了 apple 推薦的 MVC 模式做為代碼設(shè)計模式.面向?qū)ο蟮乃枷胍策\用的淋漓盡致.代碼邏輯非常的清晰,雖然代碼量并不是很大,代碼也不是很復(fù)雜,但是我覺得它的代碼習(xí)慣,封裝的思想都值得學(xué)習(xí).然后把一些值得說一說的代碼拿出來寫點東西.

雖然增加了一些類的文件,但是代碼層次更加的清晰明了,一個數(shù)據(jù)處理的類,然后把數(shù)據(jù)分發(fā)下去.感覺很順暢.

為了代碼的簡潔明了,這個項目創(chuàng)建了一個遵守 TableViewdataSource 的類來管理tableview的數(shù)據(jù)源.因為還需要遵守 ElementsDataSource ,所以自定義屬性覆蓋系統(tǒng)的 dataSource.

@interface ElementsTableViewController : UITableViewController

@property (nonatomic,strong) id<ElementsDataSource, UITableViewDataSource> dataSource;

ElementsDataSource 是一個協(xié)議,主要是用來自定義不同數(shù)據(jù)源的 tableview 的不同配置.

@protocol ElementsDataSource <NSObject>
 
@required

// these properties are used by the view controller
// for the navigation and tab bar
@property (readonly) NSString *name;
@property (readonly) NSString *navigationBarName;
@property (readonly) UIImage *tabBarImage;

// this property determines the style of table view displayed
@property (readonly) UITableViewStyle tableViewStyle;

// provides a standardized means of asking for the element at the specific
// index path, regardless of the sorting or display technique for the specific
// datasource
- (AtomicElement *)atomicElementForIndexPath:(NSIndexPath *)indexPath;

@optional

// this optional protocol allows us to send the datasource this message, since it has the 
// required information
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

@end

自定義的ElementsSortedByAtomicNumberDataSource 遵守UITableViewDataSource 就可以做為 tableview 的數(shù)據(jù)源了.


@import UIKit;

#import "ElementsDataSourceProtocol.h"

@interface ElementsSortedByAtomicNumberDataSource : NSObject<UITableViewDataSource,ElementsDataSource>{

@end

這種dataSource 單獨一個類的方式不適合 cell 有交互需求的.

其中一個給對象排序的類值得一說

    
    NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES
                                                                    selector:@selector(localizedCaseInsensitiveCompare:)] ;
    
    NSArray *descriptors = @[nameDescriptor];
    [(self.nameIndexesDictionary)[aKey] sortUsingDescriptors:descriptors];

要對數(shù)組中的對象進行排序,而數(shù)組中含有多個對象,要根據(jù)對象的其中一個屬性進行排序的時候,就可以把這個屬性做為 NSSortDescriptor 的 Key , 把數(shù)組重新排列.

關(guān)于這個對象的用法在下面的推薦拓展閱讀里面說的非常全面.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評論 4 61
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,602評論 30 472
  • 今晚,失眠。 回想初識,美妙。 一起的經(jīng)歷,坎坷。 漫長的過程,堅定。 然而 當(dāng)一切順理成章后,彼此厭惡。 初識時...
    臘月綠閱讀 203評論 0 0
  • 孩子說每天要在電腦上聽一個故事,手機聽叔叔講一個故事(群里4號叔叔),再聽我講一個故事,閱讀形式多樣化呀。 ...
    7d1132dd053f閱讀 238評論 0 0
  • 怎么區(qū)分主音,上主音,中音,下屬音,屬音?##### 所謂主音、上主音之類的,是音樂中樂音的樂名。樂名就是根據(jù)某一...
    觀茉閱讀 57,953評論 0 7

友情鏈接更多精彩內(nèi)容