在我的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