Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 5.63 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. UITableView with custom UITableViewCell not displaying properly
  2. Thread 0:
  3. 0   libSystem.B.dylib               0x310932b8 semaphore_wait_trap + 8  
  4. 1   libSystem.B.dylib               0x310c0b46 semaphore_wait + 2  
  5. 2   libSystem.B.dylib               0x3116a7c4 _dispatch_semaphore_wait_slow + 296  
  6. 3   libSystem.B.dylib               0x31169cb4 _dispatch_barrier_sync_f_slow + 128  
  7. 4   CoreFoundation                  0x3042bb70 __CFMachPortPerform + 92  
  8. 5   CoreFoundation                  0x304236f8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20  
  9. 6   CoreFoundation                  0x304236bc __CFRunLoopDoSource1 + 160  
  10. 7   CoreFoundation                  0x30415f76 __CFRunLoopRun + 514  
  11. 8   CoreFoundation                  0x30415c80 CFRunLoopRunSpecific + 224  
  12. 9   CoreFoundation                  0x30415b88 CFRunLoopRunInMode + 52  
  13. 10  GraphicsServices                0x31eec4a4 GSEventRunModal + 108  
  14. 11  GraphicsServices                0x31eec550 GSEventRun + 56  
  15. 12  UIKit                           0x313cf322 -[UIApplication _run] + 406  
  16. 13  UIKit                           0x313cce8c UIApplicationMain + 664  
  17. 14  Order Remote                    0x00002a02 0x1000 + 6658  
  18. 15  Order Remote                    0x000029cc 0x1000 + 6604
  19.        
  20. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  21. {
  22.     MenuItemCell *cell = (MenuItemCell *)[tableView dequeueReusableCellWithIdentifier:@"MenuItemCell"];
  23.     if (cell == nil)
  24.     {
  25.         cell = [[[MenuItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MenuItemCell"] autorelease];
  26.         DLog( @"MenuViewController::cellForRowAtIndexPath: allocating memory for a MenuItemCell" );
  27.     }
  28.     else
  29.     {
  30.         DLog( @"MenuViewController::cellForRowAtIndexPath: dequeuing an existing MenuItemCell for reuse" );
  31.     }
  32.  
  33.     cell.accessoryType =  UITableViewCellAccessoryNone;
  34.  
  35.     // only display the item modifiers view if the item has one or more modifiers
  36.     if( [[selectedCategory getModifiers] count] > 0 )
  37.     {
  38.         cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton;
  39.     }
  40.  
  41.     // Configure the cell.
  42.     cell.textLabel.text = [[items objectAtIndex:indexPath.row] name];
  43.  
  44.     // build a mutable string from the modifiers and set the text to the result in the detail label
  45.     NSMutableString* modifiers = [[NSMutableString alloc] initWithString:@""];
  46.     Item* i = [items objectAtIndex:indexPath.row];
  47.  
  48.     for( ItemModifier* modifier_iterator in [i modifiers] )
  49.     {
  50.         [modifiers appendString: [modifier_iterator name]];
  51.         [modifiers appendString: @" "];
  52.     }
  53.  
  54.     cell.detailTextLabel.text = modifiers;
  55.  
  56.     [modifiers release];
  57.     modifiers = nil;
  58.  
  59.     [cell setItem: i];
  60.  
  61.     return cell;
  62. }
  63.        
  64. - (void) setItem: (Item*) item;
  65. - (void) resetItemCount;
  66.  
  67. @end
  68.  
  69. @implementation MenuItemCell
  70.  
  71. - (id) initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString*) reuseIdentifier
  72. {
  73.     if( (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) )
  74.     {
  75.         itemCount = [[UILabel alloc] initWithFrame:CGRectZero];
  76.         [[self contentView] addSubview:itemCount];
  77.     }
  78.  
  79.     return self;
  80. }
  81.  
  82. - (void) resetItemCount
  83. {
  84.     [itemCount setText: @""];
  85.     [self setNeedsDisplay];
  86. }
  87.  
  88. - (void) layoutSubviews
  89. {
  90.     [super layoutSubviews];
  91.  
  92.     float inset = 10.0;
  93.     CGRect bounds = [[self contentView] bounds];
  94.     float h = bounds.size.height;
  95.     float w = bounds.size.width;
  96.  
  97.     CGRect accessoryViewBounds;
  98.  
  99.     if( !self.accessoryView )
  100.     {
  101.         accessoryViewBounds = CGRectZero;
  102.     }
  103.     else
  104.     {
  105.         accessoryViewBounds = self.accessoryView.bounds;
  106.     }
  107.  
  108.     CGRect textLabelBounds = self.textLabel.bounds;
  109.  
  110.     CGRect innerFrame = CGRectMake( round( inset + w / 2), textLabelBounds.origin.y, round( w / 2 - accessoryViewBounds.size.width - inset * 3 ), h - 1 );
  111.  
  112.     if( ( innerFrame.size.height >= bounds.size.height ) || ( innerFrame.size.width >= bounds.size.width ) )
  113.     {
  114.         [NSException raise:@"inner frame for MenuItemCell is larger than the contentView bounds" format:@"MenuItemCell (w: %d h: %d) contentView (w: %d h: %d)", innerFrame.size.width, innerFrame.size.height, bounds.size.width, bounds.size.height];
  115.     }
  116.  
  117.     // move the rectange to the right side of the cell
  118.     itemCount.textAlignment = UITextAlignmentRight;
  119.     itemCount.textColor = [UIColor redColor];
  120.     itemCount.highlightedTextColor = [UIColor whiteColor];
  121.     itemCount.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
  122.  
  123.     [itemCount setFrame: innerFrame];
  124. }
  125.  
  126. - (void) setItem: (Item*) item
  127. {
  128.     if( [item count] > 0 )
  129.     {
  130.         [itemCount setText: [NSString stringWithFormat: @"%d", [item count]]];
  131.     }
  132.     else
  133.     {
  134.         [itemCount setText: @""];
  135.     }
  136.  
  137.     [self setNeedsDisplay];
  138.  
  139.     DLog( @"MenuItemCell::itemCount %@ %d", [item name], [item count] );
  140. }
  141.  
  142. - (void) dealloc
  143. {
  144.     [itemCount release];
  145.     itemCount = nil;
  146.  
  147.     [super dealloc];
  148. }
  149.  
  150. @end
  151.        
  152. ...
  153.  
  154. @interface MenuViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
  155. {
  156.     IBOutlet UITableView* itemsTable;
  157.     NSMutableArray* items;
  158.  
  159.     Category* selectedCategory;
  160.     Order* editingOrder;
  161.     NSIndexPath* selectedRow;
  162.  
  163.     BOOL addItemToExistingOrder;
  164. }
  165.  
  166. -(id)initWithCategory: (Category*)category;
  167. -(IBAction)addItem: (id)sender;
  168.  
  169. @property (assign) Category* selectedCategory;    
  170. @property (assign) NSMutableArray* items;    
  171. @property (assign) Order* editingOrder;    
  172. @property (assign) BOOL addItemToExistingOrder;    
  173.  
  174. @implementation
  175. - (void)viewDidUnload
  176. {    
  177.     [super viewDidUnload];
  178.  
  179.     [selectedRow release];
  180.     selectedRow = nil;
  181.  
  182.     [itemsTable release];
  183.     itemsTable = nil;
  184. }