iOS - 如何以编程方式向 UITableView 添加两行开关?
我是一个iOS开发新手。我正在尝试使用以下代码以编程方式将两个开关添加到表中 -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section== 0){
fooddrinkSettingSwitch=[[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[fooddrinkSettingSwitch addTarget:self action:@selector(preferenceSettingAction:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:fooddrinkSettingSwitch];
cell.accessoryView = fooddrinkSettingSwitch;
apparelSettingSwitch=[[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[apparelSettingSwitch addTarget:self action:@selector(preferenceSettingAction:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:apparelSettingSwitch];
cell.accessoryView = apparelSettingSwitch;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//get the dictionary object
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"SettingTitle"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
但是,它显示 Food &饮料作为开关,服装作为纽带。我做错了什么?
I am an iOS development newbie. I am trying to add two switches to a table programmatically using the following code -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section== 0){
fooddrinkSettingSwitch=[[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[fooddrinkSettingSwitch addTarget:self action:@selector(preferenceSettingAction:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:fooddrinkSettingSwitch];
cell.accessoryView = fooddrinkSettingSwitch;
apparelSettingSwitch=[[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[apparelSettingSwitch addTarget:self action:@selector(preferenceSettingAction:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:apparelSettingSwitch];
cell.accessoryView = apparelSettingSwitch;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//get the dictionary object
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"SettingTitle"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
However, it displays Food & Drink as a switch and Apparel as a link. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法的一个问题是
UITableViewCell
只能有一个accessoryView
。您可能想要创建UITableViewCell
的自定义子类来实现您所追求的目标。One problem with your approach is that a
UITableViewCell
can have only oneaccessoryView
. You probably want to make a custom subclass ofUITableViewCell
to achieve what you're after.