0
从tableview中的行我有一个的tableView的顶部,我想改变加载到表中的数据的分段控制。分段控件有一天,一周,一个月,一年的按钮。如果你点击其中一个按钮,它应该只显示适当时间段的表格数据。现在,当我移动按钮时,为了在表格视图中添加额外的数据,但是当我从年,向后,月,到周,向后工作时,这很好。它不会从tableview中删除该行。删除基于IBAction为
的.M的代码如下......我还知道我的if/else if语句是可怕的,但是当我尝试并将它放入自己的功能,它与细胞返回此方法的问题。如果有人能给我一个更好的方式来做到这一点,那将是非常棒的!
- (IBAction)segmentTimePicker:(id)sender {
// the segmented control was clicked, handle it here
NSLog(@"segment clicked %d", [segmentedControl selectedSegmentIndex]);
self.tableView.reloadData;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// A date formatter for the creation date.
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
}
//A number formatter for the latitude and longitude
static NSNumberFormatter *numberFormatter = nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:3];
}
static NSString *CellIdentifier = @"Cell";
//Dequeue or create a new cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UITableViewCellStyleSubtitle;
}
//Calc how many days have passed since today, only display the right days for this segmented selector
unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
// Get the event corresponding to the current index path and configure the table view cell.
// Configure the cell...
Feeding *food = (Feeding *)[eventsArray objectAtIndex:indexPath.row];
//Check how many days have gone by for
NSDateComponents *comps = [gregorian components:unitFlags fromDate:[food feedingDate] toDate:[NSDate date] options:0];
int days = [comps day];
//Logic to test which segment is picked, need to tie it to the segmentedControl selector
if (segmentedControl.selectedSegmentIndex==0) {
if (days<=1) {
if([[food feedingType]isEqualToString:@"Bottle"]) {
NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]];
cell.textLabel.text= string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if ([[food feedingType]isEqualToString:@"Breast"]) {
NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if([[food feedingType]isEqualToString:@"Solid"]) {
NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else {
NSLog(@"Something went wrong with this food type '%@'",[food feedingType]);
}
}
else {
NSLog(@"Do nothing since its older then a day");
}
}
else if(segmentedControl.selectedSegmentIndex==1) {
if (days<=7) {
if([[food feedingType]isEqualToString:@"Bottle"]) {
NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]];
cell.textLabel.text= string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if ([[food feedingType]isEqualToString:@"Breast"]) {
NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if([[food feedingType]isEqualToString:@"Solid"]) {
NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else {
NSLog(@"Something went wrong with this food type '%@'",[food feedingType]);
}
}
else {
NSLog(@"Do nothing since its older then a week");
}
}
else if(segmentedControl.selectedSegmentIndex==2) {
if (days<=30) {
if([[food feedingType]isEqualToString:@"Bottle"]) {
NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]];
cell.textLabel.text= string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if ([[food feedingType]isEqualToString:@"Breast"]) {
NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if([[food feedingType]isEqualToString:@"Solid"]) {
NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else {
NSLog(@"Something went wrong with this food type '%@'",[food feedingType]);
}
}
else {
NSLog(@"Do nothing since its older then a month");
}
}
else {
if([[food feedingType]isEqualToString:@"Bottle"]) {
NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]];
cell.textLabel.text= string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if ([[food feedingType]isEqualToString:@"Breast"]) {
NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else if([[food feedingType]isEqualToString:@"Solid"]) {
NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]];
cell.textLabel.text = string;
cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]];
}
else {
NSLog(@"Something went wrong with this food type '%@'",[food feedingType]);
}
}
return cell;
}
我会直接评论这个问题,但我认为我没有足够的声望。 – Justin 2010-07-25 22:36:20
- (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView { \t返回1; } - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{ 返回[eventsArray计数]; } – adam0101 2010-07-26 00:52:39
你有没有改变eventsArray的内容是什么?使用数组支持UITableView并不罕见,但您的方法是独一无二的。我建议重写'cellForRowAtIndexPath:'来盲目地显示eventsArray中的任何事件。然后,可以在选定的段更改时设置eventsArray的内容,然后调用'reloadData'。 – Justin 2010-07-26 01:10:31