菜鸟教程小白 发表于 2022-12-12 16:52:34

objective-c - 从自定义 UITableViewCell 中获取值


                                            <p><p>我有一个具有 UISegmentedControl 对象的自定义 UITableViewCell。我的 UITableView(问卷形式)有 6 个这个自定义单元格(6 个分段控件)。我无法获取分段控件的 <code>selectedSegmentIndex</code>。我的猜测是在表格生成后释放单元格。我使用以下代码获取值:</p>

<pre><code>MyCustomCell *q1 = (MyCustomCell *)];

int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;
</code></pre>

<p>无论选择的索引是什么,<code>int segmentIndex</code> 总是给出相同的 <code>0</code> 值。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您必须使用正确的方法来初始化您要使用的节和行的索引路径,否则 <code>row</code> 和 <code>section</code> 属性将无法正确设置,并且你每次都会得到相同的单元格:<br/>
<code>+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;</code>. </p>

<p>代码如下:</p>

<pre><code>// getting the cell at third row of first section
NSIndexPath *ip = ;
MyCustomCell *q1 = (MyCustomCell *);
int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;
</code></pre>

<p>您也可以咨询:<a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/NSIndexPath_UIKitAdditions/Reference/Reference.html" rel="noreferrer noopener nofollow">NSIndexPath UIKit Additions Reference</a>了解更多信息。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 从自定义 UITableViewCell 中获取值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/6818806/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/6818806/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 从自定义 UITableViewCell 中获取值