Search code examples
iosobjective-cuicolor

NSArray giving me weird warning when in tableView function


Im using this constant to convert rgb to a UIColor

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

As i use this within my

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

like so

cell.textLabel.textColor = UIColorFromRGB(colours[indexPath.row]);`

And colours is defines as :

colours = [[NSArray alloc]
           initWithObjects: @[@0x808000,@0x008080,@0x9999FF,@0x008080,@0x993366,@0xFFFF99,@0xFF8080,@0x0066CC,@0x008080,@0xFF99CC,@0x3366FF,@0x99CC00,@0x969696,@0xFF9900,@0x993300] , nil ];

i get an error Invalid operands to binary expression ('id' and int) and a warning bitsmasking for introspection of Objective-C object pointers is Strongly discouraged

Any help?


Solution

  • i found the solution in this way:

    1. First Intitalize array of color code in the following way:

      colors = [[NSArray alloc]
             initWithObjects: @"0x808000",@"0x008080",@"0x9999FF",@"0x008080",@"0x993366",@"0xFFFF99",@"0xFF8080",@"0x0066CC",@"0x008080",@"0xFF99CC",@"0x3366FF",@"0x99CC00",@"0x969696",@"0xFF9900",@"0x993300", nil ];
      
    2. Second in the UITableView's CellForRowAtIndexPath method. write the following code:

      NSString *colorCode = [colors objectAtIndex:indexPath.row];
      
      // Convert Color to RGB
      NSScanner *scanner2 = [NSScanner scannerWithString:colorCode];
      unsigned baseColor1;
      [scanner2 scanHexInt:&baseColor1];
      CGFloat listviewred   = ((baseColor1 & 0xFF0000) >> 16) / 255.0f;
      CGFloat listviewgreen = ((baseColor1 & 0x00FF00) >>  8) / 255.0f;
      CGFloat listviewblue  =  (baseColor1 & 0x0000FF) / 255.0f;
      
    3. Give ColorCode to cell text color like below method:

      Cell.textLable.textColor = [UIColor colorWithRed:listviewred green:listviewgreen blue:listviewblue alpha:1.0];
      

    please check and let me know.