在我的cellForRowAtIndexPath方法中,我想動態創建一些按鈕並將它們放在單元格中彼此相鄰。我遇到的問題是sizeToFit方法總是把x座標置於0位置。有沒有辦法將位置偏移到前一個按鈕的旁邊?在TableView行中偏移UIButton框架
取而代之:。我得到這個:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UIButton *buttonClean = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonClean addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[buttonClean setTitle:@"Clean Cup" forState:UIControlStateNormal];
[buttonClean sizeToFit];
[cell addSubview:buttonClean];
UIButton *buttonAroma = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonAroma addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[buttonAroma setTitle:@"Aroma" forState:UIControlStateNormal];
[buttonAroma sizeToFit];
[cell addSubview:buttonAroma];
工作完美。 – wiznaibus 2011-04-17 04:37:59