UITutViewCell中的UIButton setImage崩溃应用程序

时间:2014-06-19 19:44:42

标签: ios objective-c uitableview uibutton

截至目前,我的桌子加载了三个单元格。两个单元格的标签包括字符串“Test”。当我第一次搜索“测试”时,会显示预期的单元格,并显示正确的图像,标签文本和按钮背景。什么都没有重叠。到目前为止那里很好。

然而,当我取消搜索时,应用程序会立即崩溃并返回并再次搜索“测试”(一旦我输入最后一个字符“t”)。

我收到此错误:

-[UIImageView setImage:forState:]: unrecognized selector sent to instance 0x9128140
2014-06-19 14:12:19.982 appName[5171:60b] *** Terminating app due to uncaught exception      'NSInvalidArgumentException', reason: '-[UIImageView setImage:forState:]: unrecognized selector sent to instance 0x9128140'

我确定实例是UIButton,而不是单元格中的UIImageView。我读了其他答案,将100添加到按钮标签,但没有任何改变。顺便说一句,我搜索时按钮标签是正确的。 (我检查了每个按钮点击的提醒)

下面是我的细胞代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// register cell identifier from custom cell NIB
static NSString *CellIdentifier = @"FriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Avatar settings
UIImageView *imvAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 45, 45)];
[imvAvatar setImage:[UIImage imageNamed:@"btnAvatar2.png"]];
imvAvatar.layer.cornerRadius = imvAvatar.frame.size.height/2;
imvAvatar.layer.masksToBounds = YES;

// Befriend Button settings
UIButton *btnBefriend = [[UIButton alloc] initWithFrame:CGRectMake(281, 14, 36, 22)];
[btnBefriend addTarget:self action:@selector(btnBefriendPressed:event:) forControlEvents:UIControlEventTouchUpInside];

if (tableView == self.searchDisplayController.searchResultsTableView) {
    friend = [searchResults objectAtIndex:indexPath.section];
} else {
    friend = [arrFriends objectAtIndex:indexPath.section];
}

// Collect friend info
NSString *user_id = (NSString *)[friend objectAtIndex:0];    // user id
NSString *username = (NSString *)[friend objectAtIndex:1];   // username
NSString *fName = (NSString *)[friend objectAtIndex:2];      // first name
NSString *lName = (NSString *)[friend objectAtIndex:3];      // last name
NSString *full_name = (NSString *)[friend objectAtIndex:4];  // full name
UIImage *picture = (UIImage *)[friend objectAtIndex:5];      // picture (img)
NSString *type = (NSString *)[friend objectAtIndex:6];       // type
NSString *arrIndex = (NSString *)[friend objectAtIndex:7];   // arrFriends index

// configure cell
if (!cell) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    // set width depending on device orientation
    cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, tableView.frame.size.width, cell.frame.size.height);

    // Name settings
    UILabel *lblName = [[UILabel alloc] initWithFrame:(CGRectMake(60, 3, 215, 45))];
    [lblName setFont:[UIFont systemFontOfSize:14]];

    // Update name, status, picture, befriend button
    lblName.text = full_name;                           // full name
    imvAvatar.image = picture;                          // picture (img)
    if ([type isEqualToString:@""]) {
        [btnBefriend setImage:[UIImage imageNamed:@"btnBefriend.png"] forState:UIControlStateNormal];
    }
    else {
        [btnBefriend setImage:[UIImage imageNamed:@"btnBefriended.png"] forState:UIControlStateNormal];
    }

    // Cell subviews
    imvAvatar.tag = 1;
    lblName.tag = 2;
    btnBefriend.tag = [arrIndex intValue];
    [cell.contentView addSubview:imvAvatar];
    [cell.contentView addSubview:lblName];
    [cell.contentView addSubview:btnBefriend];
    cell.clipsToBounds = YES;    
}
else {
    // Make sure images, buttons and texts don't overlap

    // avatar
    UIImageView *imvAvatar = (UIImageView *)[cell viewWithTag:1];
    imvAvatar.image = picture;
    // name
    UILabel *lblName = (UILabel *)[cell.contentView viewWithTag:2];
    lblName.text = full_name;
    // befriendbutton
    UIButton *btnBefriend = (UIButton *)[cell.contentView viewWithTag:[arrIndex intValue]];
    if ([type isEqualToString:@""]) {
        [btnBefriend setImage:[UIImage imageNamed:@"btnBefriend.png"] forState:UIControlStateNormal];
    }
    else {
        [btnBefriend setImage:[UIImage imageNamed:@"btnBefriended.png"] forState:UIControlStateNormal];
    }
}

return cell;
}

非常感谢帮助。提前谢谢!

1 个答案:

答案 0 :(得分:1)

我相信你的问题就在这一行:

UIButton *btnBefriend = (UIButton *)[cell.contentView viewWithTag:[arrIndex intValue]];

您期待UIButton,但返回的实际视图为UIImageView。因此[arrIndex intValue]可能与图片视图具有相同的标记,而该图片视图也是该单元格内容视图的子视图。