2013-05-27 42 views
0

我創建iOS應用程序,我想用一個交換機作爲附件的實現代碼如下。我找到了以下如何使用UISwitch作爲附件的示例:http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html(請參閱「響應選擇」部分)。DCRoundSwitch作爲附件

我想要做的是使用DCRoundSwitch(https://github.com/domesticcatsoftware/DCRoundSwitch),而不是UISwitch這樣我就可以自定義文本。但是,當我以示例代碼所建議的方式創建附件時,我只是拿到了圓形的旋鈕,我沒有得到整個開關。我用來添加附件的代碼是:

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"PersonCell"; 

    MyPerson *person = [self.dataController objectInListAtIndex:indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    DCRoundSwitch *switchObj = [[DCRoundSwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)]; 
    switchObj.onText = @"in"; 
    switchObj.offText = @"out"; 

    [switchObj addTarget:self action:@selector(personIn:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)]; 
    cell.accessoryView = switchObj; 

    [[cell textLabel] setText:person.name]; 
    [[cell detailTextLabel] setText:person.label]; 
    return cell; 
} 

如何使DCRoundSwitch作爲TableView附件工作?

謝謝!

+0

其中是實際的開關分配給附屬視圖的代碼?正如'cell.accessoryView = switchObj;' – charleyh

+0

ack,複製/粘貼錯誤。更新了包含該代碼的代碼。希望它是一致的 - 我一直在玩代碼。 – nojo

回答

0

我想你寬度僅僅是縮小 - 嘗試像60或70的寬度(默認寬度爲UISwitch在故事板是79)。

+0

嗯。這會使圓圈變大,但不會使滑塊的其餘部分出現。我想知道是否將它作爲附件使用 - 正常配件(複選標記,>等)非常狹窄 - 不知道如果我可以強制配件更寬,如果這可以允許滑塊部分出現。 – nojo

+0

@nojo,我不知道你爲什麼看到這個結果 - 我用它作爲輔助視圖,使用幾乎相同的代碼來處理你的結果,並且它工作正常(大小爲65,22) 。 – rdelmar

+0

65,22做到了。謝謝! – nojo