Iphone 如何更改UI开关在TableView中的位置
我有一个桌面视图,里面有UI开关。开关稍微离开屏幕。如何正确显示 这是我显示开关的代码Iphone 如何更改UI开关在TableView中的位置,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我有一个桌面视图,里面有UI开关。开关稍微离开屏幕。如何正确显示 这是我显示开关的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"POICell"; UITableViewCell *cell = [tableView dequeueReus
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"POICell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell){
//set the cell text to the
cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
NSString *toggle = [self.toggleArray objectAtIndex:[indexPath row]];
//add switch
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
if ([toggle isEqualToString: @"OFF"]) {
[switchView setOn:NO animated:NO];
}else{
[switchView setOn:YES animated:NO];
}
[switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
// Configure the cell...
}
return cell;
}
我认为默认accessoryView的位置不能毫无问题地移动,因为TableViewCell会在许多情况下重新调整其大小
因此,将其添加为子视图可能是更好的主意,而不是尝试“破解”默认行为。与其将UISwitch添加到标准单元格,不如创建一个包含文本标签和开关的自定义UITableViewCell 但是在看过苹果的文档之后,你想要实现的似乎是一个有效的方法
您的代码看起来已经很像这样了,所以问题:我不知道为什么UISwitch没有按预期显示。与您的问题无关:使用NSString的
IsequalString:
而不是=
。您可能希望匹配字符串内容,而不是检查指针是否相等。哦,是的,有人前几天向我指出:if(!cell)
语句故意丢失了,或者您使用的是静态单元格还是其他什么?另外,您的单元格宽度是否为“标准”?意思是,你在什么地方更改了它吗?没有,我没有设置单元格大小,在创建*切换时,内存泄漏(不是ARC,但仍然…)。将这两行合并为一行:NSString*toggle=[self.toggleray objectAtIndex:[indexPath行]]实际上,将其添加为单元格的子视图是个坏主意。有些观点是针对这类事情的,所以我会试试这个。你能解释一下怎么做吗?