ios - 如何在 UITableView Cell 中以编程方式创建 n 个 UIButton?
<p><p>我正在尝试创建一个具有 n 个按钮的 <code>UITableView</code> 取决于后端 JSON 数据。</p>
<p>我附上了一张图片,我知道如何在 <code>UITableViewCell</code> 上创建 UIButton,但我不知道如何将它们正确放置在 <code>UITableViewCell</code> 中。</p>
<p><code>UIButton</code> 用于 <code>UITableViewCell</code></p>
<pre><code>UIButton *continuebtn = [initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
];
;
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = .CGColor;
forState:UIControlStateNormal];
</code></pre>
<p> <a href="/image/ql6A3.png" rel="noreferrer noopener nofollow"><img src="/image/ql6A3.png" alt="enter image description here"/></a> </p>
<p>如何在 <code>UITableViewCell</code> 上放置 'n' 个 <code>UIButton</code> ? <code>UIButton</code> 宽度取决于它的文本内容</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果您想在单元格中垂直放置按钮,请使用以下建议:</p>
<p><code>UITableviewCell</code> 的高度取决于按钮的数量。实现 <code>heightForRowAtIndexPath</code> 方法如下:</p>
<pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0f + (buttonsArray.count*buttonHeight+buttonsHeightSeparator);
//change 100.0f with height that is required for cell without buttons
//buttonHeight is a static float representing value for height of each button
//buttonHeightSeparator is a static float representing separation distance between two buttons
}
</code></pre>
<p>在您的 <code>cellForRowAtIndexPath</code> 方法中,您可以使用以下代码创建按钮:</p>
<pre><code>for(int i=0; i<buttonsArray.count; i++) {
UIButton *continuebtn = [initWithFrame:CGRectMake(10, 100+i*(buttonHeight+buttonsHeightSeparator), view1.frame.size.width-20, 40)];
];
;
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = .CGColor;
forState:UIControlStateNormal];
; //add target to receive button tap event
; //to identify button
; //add button to cell
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 UITableView Cell 中以编程方式创建 n 个 UIButton?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33496982/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33496982/
</a>
</p>
页:
[1]