这是本人的代码:
- (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];
[_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];
//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];