UITableViewCell分隔符拒绝消失,NSTextAlignmentRight不生效
问题描述:
我在我的StoryBoard应用程序中使用UITableView。 改变颜色工作,方向不起作用,分隔线仍然出现。UITableViewCell分隔符拒绝消失,NSTextAlignmentRight不生效
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"row320x44.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row320x44.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row320x44.png"]];
}
我试着在viewDidLoad中:
self.tableViewWallMessages.delegate = self;
self.tableViewWallMessages.separatorColor = [UIColor clearColor];
self.tableViewWallMessages.separatorStyle = UITableViewCellSeparatorStyleNone;
仍然一无所获。 任何想法?
答
请尝试以下操作。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//set the separator colour in the table (menu)
tableView.separatorColor = [UIColor clearColor];
// Return the number of sections.
return 1;
}
如果不工作,然后尝试添加在willDisplayCell
以下cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
或者不是使用情节串连图板,你可以在.h文件中创建编程
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *myTableView;
}
@property(nonatomic,strong)UITableView *myTableView;
.m文件中的
- (void)viewDidLoad
{
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
myTableView.dataSource = self;
myTableView.delegate = self;
[myTableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//set the separator colour in the table (menu)
tableView.separatorColor = [UIColor clearColor];
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//set the number of rows in the table (menu) as 6
int noRows = 6;
return noRows;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//set the row height for the table (menu)
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//Create a title label in the table (menu)
UILabel *titleLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
/**TITLE LABEL SETUP**/
//setup the title label
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 54.0, 77, 33)];
titleLabel.tag = TITLE_TAG;
//add the title label to the cell
[cell.contentView addSubview:titleLabel];
}
else
{
//recall the existing cell content if it has already been created
titleLabel = (UILabel*)[cell.contentView viewWithTag:TITLE_TAG];
}
//set the icon image as the image from the array
//set the menu item title from the array
titleLabel.text = @"some text";
//dont show the clicked cell as highlighted
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Code to execute when a cell is clicked on
}
答
尝试在Storyboard文件中设置分隔符值“None”。
你的代码是正确的,只是确保你已连接的UITableView出口与故事板。
完成,我仍然有分隔符。 –
我倾向于以编程方式构建我的所有表格,因为我发现它的效果更好,因为故事板不会妨碍并覆盖我要求它执行的操作。这里有一个关于故事板桌面的很好的教程,包括删除分隔线http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/希望它对你有用 –
指南实际上工作,谢谢!但我在UIViewController中有一个UITableView,它占用了屏幕的50%,我不认为我可以将UIViewController更改为UITableViewController,因为我需要在屏幕的50%剩余部分中执行另一项操作。 –