为什么本人在table view cell中添加的按钮不显示

iOS 码拜 8年前 (2016-03-29) 980次浏览
这是本人的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if([self.setArray[indexPath.row] isEqualToString:@"存储设置"])
    {
        _button = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 20)];
        _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_button setTitle:@"缓存" forState:UIControlStateNormal];
        _button.backgroundColor = [UIColor grayColor];
        _button.opaque = YES;
        _button.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:_button];
    }
    else
    {
        if(indexPath.section == 0)
        {
            cell.textLabel.text = self.setArray[indexPath.row];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else if(indexPath.section == 1)
        {
            cell.textLabel.text = self.setArray[indexPath.row+4];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
    }
    cell.textLabel.backgroundColor = [UIColor clearColor];
    
    return cell;
}

调试证明代码执行了 if([self.setArray[indexPath.row] isEqualToString:@”存储设置”])条件中代码,但不知道为什么_button没显示出来,在线急等!刚接触iOS时间不长,望会的各位大牛,不吝赐教,在线急等!

解决方案

20

你给button的title也设置一下color
[_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

20

直接加载cell上试试,根据本人以前遇到的情况,你加在content view上就算显示出来也执行不了click方法

60

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 20)];
  //button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@”缓存” forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
button.opaque = YES;
button.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:button];

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明为什么本人在table view cell中添加的按钮不显示
喜欢 (0)
[1034331897@qq.com]
分享 (0)