ios - UItableView 用 UIButton 替换复选标记
<p><p>我只想使用 <code>UIButton</code> 而不是选中标记我成功添加按钮并使其在选择行时作为复选标记但只有行选择是工作按钮不明智不是我想让它工作按钮以及单元格选择截图:</p>
<p> <a href="http://i.imgur.com/aXiBsfU.png" rel="noreferrer noopener nofollow">http://i.imgur.com/aXiBsfU.png</a> </p>
<p> <a href="http://i.imgur.com/hQddEGk.png" rel="noreferrer noopener nofollow">http://i.imgur.com/hQddEGk.png</a> </p>
<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0)
{
if (selectAll==0)
{
selectAll=1;
;
}
else
{
selectAll=0;
;
}
}
else
{
if (selectAll!=1)
{
if()
{
;
}
else
{
;
}
}
else
{
selectAll=0;
if()
{
;
}
else
{;}}};
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static const NSInteger btnTag = 1;
static NSString *MyIdentifier = @"SearchResult";
UITableViewCell *cell;
if (cell == nil)
{
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
UILabel *lblText = [initWithFrame:CGRectMake(40, 0, 400, 55)];
UIButton *btnCheck = [initWithFrame:CGRectMake(845, 10, 35, 28)];
btnCheck.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
btnCheck.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
forState:UIControlStateNormal];
forState:UIControlStateSelected];
forState:UIControlStateHighlighted];
;
btnCheck.tag = btnTag;
;
;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
if (indexPath.row==0)
{
lblText.textColor =;
lblText.font=;
}
else
{
lblText.textColor=;
lblText.font=;
}
}
else
{
if (indexPath.row==0)
{
lblText.textColor=;
lblText.font=;
}
else
{
lblText.textColor=;
lblText.font=;
}
}
lblText.text=;
lblText.textAlignment=NSTextAlignmentLeft;
if (selectAll==1)
{
;
forState:UIControlStateNormal];
}
else
{
if()
{
;
forState:UIControlStateNormal];
}
else
{
;
forState:UIControlStateNormal];
}
}
cell.textLabel.numberOfLines=0;
cell.textLabel.lineBreakMode=NSLineBreakByWordWrapping;
return cell;
}
- (void)doneBtn:(UIButton*)sender
{
UIButton *btn = (UIButton*)sender;
}
</code></pre>
<p>提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>全局声明这些变量</p>
<pre><code>BOOL isSelectedAll;
NSMutableArray *arraySelectedRows;
int allDataCount; // this may be your arrayWithData.count
</code></pre>
<p>在'viewDidLoad'中</p>
<pre><code>isSelectedAll = NO;
allDataCount = 10;
arraySelectedRows = [init];
</code></pre>
<p>在'cellForRowAtIndexPath'中</p>
<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = ;
CustomTableViewCell *cell = [initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cee"];
cell.textLabel.text = @"Test";
UIButton *buttonSelection = [initWithFrame:CGRectMake(tableView.frame.size.width - 100, 8, 50, 50)];
buttonSelection.tag = indexPath.row;
if(isSelectedAll)
{
forState:UIControlStateNormal];//Rename imageName as yours
}
else if(&&indexPath.row!=0)
{
forState:UIControlStateNormal];//Rename imageName as yours
}
else
{
forState:UIControlStateNormal];//Rename imageName as yours
}
;
;
return cell;
}
</code></pre>
<p>在“didSelectRowAtIndexPath”中</p>
<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
//select all button
if (isSelectedAll)
{
isSelectedAll = NO;
;
}
else
{
;
for (int i = 1; i<allDataCount; i++)
{
];//section number may be defer for you
}
isSelectedAll = YES;
}
}
else
{
if()
{
;
}
else
{
;
}
if ((allDataCount - 1) == arraySelectedRows.count)
{
isSelectedAll = YES;
}
else
{
isSelectedAll = NO;
}
}
;
}
</code></pre>
<p>如果你需要在按钮操作中做同样的事情</p>
<pre><code>-(void)selectionButtonAction:(UIButton*)button
{
if (button.tag == 0)
{
//select all button
if (isSelectedAll)
{
isSelectedAll = NO;
;
}
else
{
;
for (int i = 1; i<allDataCount; i++)
{
];//section number may be defer for you
}
isSelectedAll = YES;
}
}
else
{
NSIndexPath *buttonIndexPath = ;//section number may be defer for you
if()
{
;
}
else
{
;
}
if ((allDataCount - 1) == arraySelectedRows.count)
{
isSelectedAll = YES;
}
else
{
isSelectedAll = NO;
}
}
;
}
</code></pre>
<p>“arraySelectedRows”包含选定行的索引路径</p></p>
<p style="font-size: 20px;">关于ios - UItableView 用 UIButton 替换复选标记,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31875235/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31875235/
</a>
</p>
页:
[1]