在写一个表格来呈现和组织应用程序设置区域时,我总是觉得有点“困惑”。我认为更好的方法是使用静态表,但我发现自定义它非常困难(特别是因为我无法自定义分组 View 单元格)。
所以我经常会得到一个带有数据源的公用表。我的疑问与应该直接显示选项信息的单元格有关。
假设我需要两种细胞。
我的疑问主要是:
cellForItemAtIndex
期间只显示或隐藏标签或开关吗?过程? 您能否分享一些关于如何实现设置的想法/代码示例?
最佳答案
这个答案基于我在自己的应用程序中所做的事情。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
cell = [[TSOptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if (indexPath.row == 0) /* or whatever index you want to set the switch or label at */
{
UISwitch * switch = [[UISwitch alloc] initWithFrame:frame];
[switch addTarget:self action:@selector(toggleSwitch:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
/* create your switch and add it's target and action (method that's called when it is toggled) */
// Set the value of the switch
switch = [[NSUserDefaults standardUserDefaults] boolForKey:@"key"];
[cell setAccessoryView:switch];
}
}
-(void)toggleSwitch:(id)sender
{
/* handle user interaction somehow */
}
关于ios - 为设置创建一个 UITableView。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24311393/