1
我試着添加一個按鈕到一個新的細胞在tableview中,例如,我需要在小區「1」按鈕,如果暗淡不爲0UITableView中添加新的單元格按鈕
但是我不能去做吧。請賜教。
- (void)viewDidLoad
{
[super viewDidLoad];
_displaynum = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
_displayID = [NSArray arrayWithObjects:@"ID = 1",@"ID = 2",@"ID = 3", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_displaynum count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *lightIdentifier = @"lightIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:lightIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:lightIdentifier];
}
UISwitch *switchLights = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)];
cell.textLabel.text = [_displayLights objectAtIndex:indexPath.row];
cell.detailTextLabel.text =[_displayLightsID objectAtIndex:indexPath.row];
cell.accessoryView = switchLights;
switchLights.on = NO;
int dim = 1;
if ((dim =! 0)) {
NSArray *insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:1 inSection:0],
nil];
NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],nil];
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
return cell;
}
}
感謝上的tableView的解釋,我已經逐漸接近的結果。但按鈕似乎與第二個單元覆蓋。 – aka
您需要根據您的細胞高度調整按鈕框架。你也可以增加細胞高度。讓我知道你是否需要任何幫助。 – Abhinav
我對按鈕框架有所瞭解,但按鈕與第二個對象疊加確切。對於exp,單元格1(對象1),按鈕,單元格2(對象2)。在此先感謝 – aka