如何在 uitableview-iphone 中正确设置 ui 元素的标签
我有一个表视图控制器,其中一个标签和一个开关显示在一行中。要添加这样的元素,我使用下面的方法(建议避免行中的数据混淆)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch* testSwitch = [[UISwitch alloc] initWithFrame:
CGRectMake(200,
0.5 * (cell.frame.size.height-30 ),
30, 30)];
testSwitch.tag = 2;//If this is a dynamic value, it only works for 14 rows
[cell.contentView addSubview:testSwitch];
[testSwitch release];
}
UISwitch *switch1 = (UISwitch*) [cell viewWithTag:2];
//cannot set a dynamic value here
switch1.on = [<array value> boolValue];
我需要标签是一个动态值,具体取决于当前行上的数据,以便我可以在值更改时添加操作方法,并根据模型进行相关更改。 但是,由于标记是在第一部分中设置的,因此如果我将其设为动态,则只会设置 14 个值(即屏幕上显示的行数)。 我该怎么做才能在此开关的所有行上存储动态值以供其目标操作方法检索?
谢谢..
--编辑--- 将代码更改为
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch* testSwitch = [[UISwitch alloc] initWithFrame:
CGRectMake(200,
0.5 * (cell.frame.size.height-30 ),
30, 30)];
testSwitch.tag = indexPath.row;//If this is a dynamic value, it only works for 14 rows
[cell.contentView addSubview:testSwitch];
[testSwitch release];
}
UISwitch *switch1 = (UISwitch*) [cell viewWithTag:indexPath.row];
switch1.on = someboolValue; //CRASH as switch1 points to UItablviewcell and not UISwitch.
I have a tableview controller, with a label and a switch displayed in a row. To add elements like this, I am using the approach below(as is recommended to avoid the data in the row getting mixed up).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch* testSwitch = [[UISwitch alloc] initWithFrame:
CGRectMake(200,
0.5 * (cell.frame.size.height-30 ),
30, 30)];
testSwitch.tag = 2;//If this is a dynamic value, it only works for 14 rows
[cell.contentView addSubview:testSwitch];
[testSwitch release];
}
UISwitch *switch1 = (UISwitch*) [cell viewWithTag:2];
//cannot set a dynamic value here
switch1.on = [<array value> boolValue];
I need the tag to be a dynamic value depending on the data on the current row so that I can add an action method when the value changes, and make according model related changes.
However, since the tag is set in the first section, if I make that dynamic, only 14 values(which are the number of rows displayed on the screen) are set.
What do I do so that I can store a dynamic value on all the rows of this switch for its target action method to retrieve?
Thanks..
--Edit---
Changed code to
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch* testSwitch = [[UISwitch alloc] initWithFrame:
CGRectMake(200,
0.5 * (cell.frame.size.height-30 ),
30, 30)];
testSwitch.tag = indexPath.row;//If this is a dynamic value, it only works for 14 rows
[cell.contentView addSubview:testSwitch];
[testSwitch release];
}
UISwitch *switch1 = (UISwitch*) [cell viewWithTag:indexPath.row];
switch1.on = someboolValue; //CRASH as switch1 points to UItablviewcell and not UISwitch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您只有一个部分。
好的,现在听起来您遇到了一个问题
[cell viewWithTag:indexPath.row]
正在定位单元格而不是它的子视图,因此假设您只有一个 uiswitch 子视图:
assuming you only have one section.
ok, it now sounds like you are having a problem that
[cell viewWithTag:indexPath.row]
is locating the cell and not it's subview so assuming you only have one uiswitch subview: