Iphone 添加按钮以分隔单元格
我需要添加按钮来分隔单元格 黄色圆圈中的按钮 我可以添加到所有单元格,如Iphone 添加按钮以分隔单元格,iphone,objective-c,uitableview,uibutton,Iphone,Objective C,Uitableview,Uibutton,我需要添加按钮来分隔单元格 黄色圆圈中的按钮 我可以添加到所有单元格,如[cell addSubview:myButt]但它适用于所有单元格,我只需要5、6、7、8个单元格 我是否需要编写自定义的UITableViewCell 而那个一行(黑色箭头),据我所知,它是一个部分,我如何才能创建它而不带标题,带着行(图像)并且在表格的中间?谢谢大家 顺便说一句,我的英语很抱歉只需在cellforrow方法中添加按钮: If (indexpath.row == 3 || indexpath.row =
[cell addSubview:myButt]代码>但它适用于所有单元格,我只需要5、6、7、8个单元格
我是否需要编写自定义的UITableViewCell
而那个一行(黑色箭头),据我所知,它是一个部分,我如何才能创建它而不带标题,带着行(图像)并且在表格的中间?谢谢大家
顺便说一句,我的英语很抱歉只需在cellforrow方法中添加按钮:
If (indexpath.row == 3 || indexpath.row == 4 || indexpath.row == 5 || indexpath.row == 3)
{
// add button here
// tag button here
Button.tag = indexpath.row ;
}
只需在cellforrow方法中添加按钮:
If (indexpath.row == 3 || indexpath.row == 4 || indexpath.row == 5 || indexpath.row == 3)
{
// add button here
// tag button here
Button.tag = indexpath.row ;
}
您可以按如下方式隐藏分区标题:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0f;
// or 1.0 as per your line
}
对于单元格上的按钮,必须创建一个自定义单元格类,然后在cellForRowAtIndexPath方法中使用该类。您也可以为此使用标记。您可以这样做:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:"Cell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(indexPath.section == 1)
{
if(indexPath.row>=5 && indexPath.row <=8)
{
//adding buttons to custom cell
}
}
}
-(UITableView单元格*)表视图:(UITableView*)表视图单元格的行索引路径:(nsindepath*)indepath
{
UITableViewCell*cell=[theTableView dequeueReusableCellWithIdentifier:“cell”];
如果(单元格==nil){
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
}
if(indexPath.section==1)
{
如果(indexPath.row>=5&&indexPath.row,则可以如下方式隐藏节头:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0f;
// or 1.0 as per your line
}
对于单元格上的按钮,您必须创建一个自定义单元格类,然后在cellForRowAtIndexPath方法中使用该类。您还可以为此使用标记。您可以这样做:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:"Cell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(indexPath.section == 1)
{
if(indexPath.row>=5 && indexPath.row <=8)
{
//adding buttons to custom cell
}
}
}
-(UITableView单元格*)表视图:(UITableView*)表视图单元格的行索引路径:(nsindepath*)indepath
{
UITableViewCell*cell=[theTableView dequeueReusableCellWithIdentifier:“cell”];
如果(单元格==nil){
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
}
if(indexPath.section==1)
{
如果(indexPath.row>=5&&indexPath.row您可以使用UIView
的标记
来跟踪按钮,但我确实建议使用自定义的UITableViewCell
。也就是说,下面是您可以使用的代码。它经过优化,不必反复添加相同的按钮,而是在您需要时添加,并且如果您不需要它,请将其集成到ide中
static int kButtonViewTag = 3294802;
UIButton *button = [cell.contentView viewWithTag:kButtonViewTag];
BOOL shouldDisplayButton = indexpath.row == 5 || indexpath.row == 6 || indexpath.row == 7 || indexpath.row == 8;
// If the button should be displayed and is not
//
if (shouldDisplayButton) {
// Add button here
//
if (!button) {
button = // Init your button
button.tag = kButtonViewTag;
[cell.contentView addSubview:button]
}
else if (button.isHidden) {
button.hidden = NO;
}
}
// If the button should not be displayed but is
//
else if (!shouldDisplayButton && button && !button.isHidden) {
button.hidden = NO;
}
然后你的部分:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat tableWidth = tableView.frame.size.width;
CGFloat padding = 10; // Add some padding
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(padding, 0, tableWidth - padding * 2, 1)];
[view setBackgroundColor:[UIColor greyColor]; //your background color...
return view;
}
您可以使用UIView
的标记
属性来跟踪您的按钮,但我真的建议您使用自定义的UITableViewCell
。下面是您可以使用的代码。它经过优化,可以在需要时不反复添加相同的按钮,并在不需要时隐藏它
static int kButtonViewTag = 3294802;
UIButton *button = [cell.contentView viewWithTag:kButtonViewTag];
BOOL shouldDisplayButton = indexpath.row == 5 || indexpath.row == 6 || indexpath.row == 7 || indexpath.row == 8;
// If the button should be displayed and is not
//
if (shouldDisplayButton) {
// Add button here
//
if (!button) {
button = // Init your button
button.tag = kButtonViewTag;
[cell.contentView addSubview:button]
}
else if (button.isHidden) {
button.hidden = NO;
}
}
// If the button should not be displayed but is
//
else if (!shouldDisplayButton && button && !button.isHidden) {
button.hidden = NO;
}
然后你的部分:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat tableWidth = tableView.frame.size.width;
CGFloat padding = 10; // Add some padding
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(padding, 0, tableWidth - padding * 2, 1)];
[view setBackgroundColor:[UIColor greyColor]; //your background color...
return view;
}
写一些检查,比如if(indexPath.section==1)&if(indexPath.row==0){[cell addSubview:myButt];}或者if(indexPath.row==1){[cell addSubview:myButt];}等等。这真的很简单。自己试试。幸运的是,你可以在cellforrowatinexpath:
中使用viewWithTag:
,但它很快就会变得难以维护。你应该将UITableViewCell子类化:视图逻辑属于UIView子类。写一些检查,比如if(indexPath.section==1)&if(indexPath.row==0){[cell addSubview:myButt];}如果(indexPath.row==1){[cell addSubview:myButt];}等等。这真的很简单。你自己试试。很幸运,你可以在cellForRowAtIndexPath:
中使用viewWithTag:
,但它很快就会变得难以维护。你应该将UITableViewCell子类化:视图逻辑属于UIView子类。然后,当用户上下滚动时,该按钮将被重复使用。它将被删除重复使用,但即使它不是正确的单元格,它也将始终可见。是的,您可以使用标记,但您的答案不能证明我没有否决您的答案,但我仍然认为这是不够的。然后,当用户上下滚动时,按钮将被重复使用。它将被重复使用,但即使它是正确的,它也将始终可见不是正确的单元格。是的,你可以使用标记,但你的答案不能证明我没有否决你的答案,但我仍然认为这是不够的。