我在我的應用程序中使用SplitViewController。在主ViewController中,我使用Custom表。 錯誤:原因:' - [UITableViewCellSelectedBackground setImage:]
下面是代碼: 在viewDidLoad中:
self.items = [NSMutableArray arrayWithObjects:@"About Company", @"Contact Us", @"Nearest Center", nil];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 100;
self.tableView.backgroundColor = [UIColor purpleColor];
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @" " ;
}
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#if USE_CUSTOM_DRAWING
const NSInteger TOP_LABEL_TAG = 1001;
const NSInteger BOTTOM_LABEL_TAG = 1002;
UILabel *topLabel;
UILabel *bottomLabel;
#endif
static NSString *CellIdentifier = @"Cell"; // <-- Make sure this matches what you have in the storyboard
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
#if USE_CUSTOM_DRAWING
UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];
cell.accessoryView = [[UIImageView alloc]initWithImage:indicatorImage];
const CGFloat LABEL_HEIGHT = 20;
UIImage *image = [UIImage imageNamed:@"imageA.png"];
//
// Create the label for the top row of text
//
topLabel =[[UILabel alloc]initWithFrame:CGRectMake(image.size.width + 2.0 * cell.indentationWidth,
0.5 * (atableView.rowHeight - 2 * LABEL_HEIGHT),
atableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)] ;
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the same on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//
// Create the label for the top row of text
//
bottomLabel =[[UILabel alloc]initWithFrame:CGRectMake(image.size.width + 2.0 * cell.indentationWidth,
0.5 * (atableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
atableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width, LABEL_HEIGHT)];
[cell.contentView addSubview:bottomLabel];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
//
// Create a background image view.
//
cell.backgroundView = [[UIImageView alloc] init];
cell.selectedBackgroundView =[[UIImageView alloc] init];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
#endif
}
#if USE_CUSTOM_DRAWING
else
{
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
}
topLabel.text = [self.items objectAtIndex:indexPath.row];
bottomLabel.text = [self.items objectAtIndex:indexPath.row];
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [atableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
rowBackground = [UIImage imageNamed:@"topRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:@"bottomRow.png"];
selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
}
else
{
rowBackground = [UIImage imageNamed:@"middleRow.png"];
selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
//
// Here I set an image based on the row. This is just to have something
// colorful to show on each row.
//
switch ([indexPath row]) {
case 0:
cell.imageView.image = [UIImage imageNamed:@"imageA.png"];
break;
case 1 :
cell.imageView.image = [UIImage imageNamed:@"imageB.png"];
break;
default:
break;
}
#else
//cell.text = [NSString stringWithFormat:@"Cell at row %ld.", [indexPath row]];
// Configure the cell.
cell.textLabel.text = [self.items objectAtIndex:indexPath.row]; // Set the MasterViewController's tableView row "Cell" to display selected array element
#endif
return cell;
}
這裏是,我發現了錯誤:
- [UITableViewCellSelectedBackground setImage:]:無法識別的選擇發送到實例0x7616300 2013-04-16 11:41:40.841 ICare [1144:c07] * 由於未捕獲異常而終止應用程序 'NSInvalidArgumentException',原因: ' - [UITableViewCellSelectedBackground setImage:]:發送到實例0x7616300無法識別選擇 ' *第一擲調用堆棧:(0x1532012 0x1357e7e 0x15bd4bd 0x1521bbc 0x152194e 0xa3da 0x7358fb 0x7359cf 0x71e1bb 0x71c872 0x7275d4 0x72a1c4 0x72a546 0x85ab 0x75c1c7 0x75c232 0x75c4da 0x7738e5 0x7739cb 0x773c76 0x773d71 0x77489b 0x7749b9 0x774a45 0x87a20b 0x6cb2dd 0x136b6b0 0x19ffc0 0x19433c 0x19feaf 0x76a2bd 0x6b2b56 0x6b166f 0x6b1589 0x6b07e4 0x6b061e 0x6b13d9 0x6b42d2 0x75e99c 0x6ab574 0x6ab76f 0x6ab905 0x6b4917 0x67896c 0x67994b 0x68acb5 0x68bbeb 0x67d698 0x2629df9 0x2629ad0 0x14a7bf5 0x14a7962 0x14d8bb6 0x14d7f44 0x14d7e1b 0x67917a 0x67affc 0x7的BBD 0x2995爲0x1)的libC++ abi.dylib:終止叫 拋出一個異常(LLDB)
我錯過了一些東西。但我無法解決它,因爲我是IOS開發新手。
任何幫助或建議,應該感激。謝謝。
標記一個斷點,然後你會到達錯誤來臨的確切路線 –
然後如果你仍然有一個問題理解它,在這裏發佈有問題的行。看起來你可能在一個不支持它的類型實例上使用未定義的選擇器(即方法名)。 – giorashc
另外,正如你的錯誤所說,setImage:所以檢查你的代碼,你在哪裏設置圖像。自己發現錯誤將成爲道德助推器。然後閱讀答案。 –