Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何更改UI开关在TableView中的位置_Iphone_Objective C_Ios_Xcode - Fatal编程技术网

Iphone 如何更改UI开关在TableView中的位置

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

我有一个桌面视图,里面有UI开关。开关稍微离开屏幕。如何正确显示

这是我显示开关的代码

 - (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行]]