MyTable.m
上传用户:qhdwjd2004
上传日期:2020-12-29
资源大小:362k
文件大小:5k
源码类别:

iPhone

开发平台:

Objective-C

  1. #import "MyTable.h"
  2. #import "ListView.h"
  3. #import <dirent.h>
  4. @implementation MyTable
  5. -(id)initWithFrame : (struct CGRect)rect
  6. {
  7. if((self == [super initWithFrame: rect]) != nil){
  8. colFilename = [ [ UITableColumn alloc ]
  9.             initWithTitle: @"Filename"
  10.             identifier:@"filename"
  11.             width: rect.size.width
  12.         ];
  13. [self addTableColumn: colFilename];
  14. [self setSeparatorStyle:1];
  15. [self setDelegate: self];
  16. [self setDataSource: self];
  17. [self setRowHeight: 64 ];
  18. fileList = [[NSMutableArray alloc] init];
  19. }
  20. return self;
  21. }
  22. -(void)dealloc
  23. {
  24. [ colFilename release ];
  25.     [ colType release ];
  26.     [ fileList release ];
  27. [super dealloc];
  28. }
  29. -(int)numberOfRowsInTable:(UITable *)_table
  30. {
  31.     if(state==0)
  32.     {
  33.         NSLog(@"%i",maxChapterNum);
  34.         return maxChapterNum;
  35.     }
  36.     else
  37.     {
  38.         NSLog(@"%i",[ fileList count ]);
  39.         return [ fileList count ];
  40.     }
  41. }
  42. -(UITableCell *)table:(UITable *)table
  43.     cellForRow:(int)row
  44.     column:(UITableColumn *)col
  45. {
  46.     DeletableCell *cell = [ [ DeletableCell alloc ] init ];
  47.     [ cell setTable: self ];
  48.     NSLog(@"row");
  49.     if(state==0)
  50.     {        
  51.         [ cell setTitle: [[[ NSString alloc ] initWithFormat: @"Stage %i/", row+1 ]
  52.             stringByDeletingPathExtension ]];
  53.         [ cell setImage: [UIImage applicationImageNamed:@"images/chapter.png"] ];
  54.         NSLog(@"%i",row);
  55.     }
  56.     else
  57.     {
  58.         [ cell setTitle: [ [ fileList objectAtIndex: row ]
  59.             stringByDeletingPathExtension ]];
  60.         [ cell setImage: [UIImage applicationImageNamed:@"images/map.png"] ];
  61.     }
  62.     [ cell setShowDisclosure: YES ];
  63.     [ cell setDisclosureStyle: 3 ];
  64.     return [ cell autorelease ];   
  65. }
  66. -(int)swipe:(int)type withEvent:(struct __GSEvent *)event
  67. {
  68.     if(state != 0) // 只有自定义地图才能被删除
  69.     {        
  70.         CGPoint point= GSEventGetLocationInWindow(event);
  71.         CGPoint offset = _startOffset;
  72.         
  73.         if (point.x < 100 || point.x > 200) {
  74.             point.x += offset.x;
  75.             point.y += offset.y-48;
  76.             int row = [ self rowAtPoint:point ];
  77.         
  78.             [ [ self visibleCellForRow: row column: 0 ]
  79.                _showDeleteOrInsertion: YES
  80.                withDisclosure: NO
  81.                animated: YES
  82.                isDelete: YES
  83.                andRemoveConfirmation: YES
  84.             ];
  85.         
  86.             return [ super swipe:type withEvent:event ];
  87.         } 
  88.     }
  89. }
  90. -(void)_willDeleteRow:(int)row
  91.     forTableCell:(id)cell
  92.     viaEdge:(int)edge
  93.     animateOthers:(BOOL)animate
  94. {
  95.     [ fileList removeObjectAtIndex: row ];
  96.     [ super _willDeleteRow: row forTableCell: cell viaEdge: edge
  97.       animateOthers: animate ];
  98. }
  99. -(void)tableRowSelected:(NSNotification *)notification
  100. {
  101.     int index = [ self selectedRow ];
  102.     if(selectIndex!=index)
  103.     {
  104.         selectIndex = index;
  105.     }
  106.     else
  107.     {
  108.         selectIndex = -1;
  109.         if(state ==0)
  110.         {
  111.             [(ListView *)listView itemSelected: (id)index];
  112.         }
  113.         else
  114.         {
  115.             [(ListView *)listView itemSelected: (id)[ fileList objectAtIndex: index ]];
  116.         }
  117.     }
  118. }
  119. -(int)getState
  120. {
  121.     return state;
  122. }
  123. -(void)setState:(int)st
  124. {
  125.     state = st;
  126. }
  127. -(void)setMaxChapter:(int)chapter
  128. {
  129.     if(chapter>0)
  130.        maxChapterNum = chapter;
  131.     else
  132.        maxChapterNum = 1;
  133. }
  134. -(void)setPath:(NSString *)_path
  135. {
  136. path = [ _path copy];
  137. }
  138. -(void)setView:(UIView *)_mainView
  139. {
  140.     listView = _mainView;
  141. }
  142. -(void)reloadData
  143. {
  144.     if(state ==0)
  145.     {
  146.     }
  147.     else
  148.     {
  149.         // 重置
  150.         [ fileList removeAllObjects ];
  151.      NSFileManager *fileManager = [ NSFileManager defaultManager ];
  152.         NSDirectoryEnumerator *dirEnum;
  153.         NSString *file;
  154.         BOOL isdir;
  155.         // 判断目录是否存在
  156.         if ([ fileManager fileExistsAtPath: path isDirectory:&isdir] == YES
  157.             && isdir)
  158.         {
  159.             // 加载数据
  160.             NSArray *flist = [[NSFileManager defaultManager] directoryContentsAtPath: path];
  161.             int i=0;
  162.             NSEnumerator *enumerator = [flist objectEnumerator];
  163.             id obj;
  164.             while (obj = [enumerator nextObject]) {
  165.                 file = (NSString*)obj;
  166.                 if([file hasSuffix: [[NSString alloc] initWithCString: MAP_FILE_SUFFIX ]] == YES)
  167.                 {
  168.                     [ fileList addObject: file ];
  169.                 }
  170.             }
  171.         }
  172.     }
  173.     selectIndex = -1;
  174.     [ super reloadData ];
  175. }
  176. // 删除数据
  177. -(void)itemDelete:(int)index
  178. {
  179.     NSString *dfilepath = [[[[NSString alloc] initWithCString:Custom_Map_Path] stringByExpandingTildeInPath]
  180.          stringByAppendingPathComponent:(NSString*)[ fileList objectAtIndex: index ]];
  181.     [(ListView*)listView deleteMap:dfilepath];
  182. }
  183. @end
  184. @implementation DeletableCell
  185. - (void)removeControlWillHideRemoveConfirmation:(id)fp8
  186. {
  187.     [ self _showDeleteOrInsertion:NO
  188.           withDisclosure:NO
  189.           animated:YES
  190.           isDelete:YES
  191.           andRemoveConfirmation:YES
  192.     ];
  193. }
  194. - (void)_willBeDeleted
  195. {
  196.     int row = [ table _rowForTableCell: self ];
  197.     [table itemDelete:row];
  198.     /* Do something; this row is being deleted */
  199. }
  200. - (void)setTable:(MyTable *)_table {
  201.     table = _table;
  202. }
  203. @end