减去UITableView中行的值

时间:2015-10-27 06:16:01

标签: ios objective-c uitableview

我正在制作秒表,在我的应用程序中,除了单圈时间外,一切正常。我到处寻找,但我无法弄清楚如何去做。 在我的UITableView中,每一行都包含分割时间。我实际需要的是单圈时间,即第二次分段时间 - 第一次分段时间=第一圈时间。

这是我的代码。

NSArray *arr1 = [str1 componentsSeparatedByString:@" "];
        NSString *final1 = [arr1 lastObject];
        NSArray *finalArray1 = [final1 componentsSeparatedByString:@":"];

        NSString *str2 = arrsplit[0];
        NSArray *arr2 = [str2 componentsSeparatedByString:@" "];
        NSString *final2 = [arr2 lastObject];
        NSArray *finalArray2 = [final2 componentsSeparatedByString:@":"];

        hours = [finalArray1[0] intValue] - [finalArray2[0] intValue];
        minutes = [finalArray1[1] intValue] - [finalArray2[1] intValue];
        seconds = [finalArray1[2] intValue] - [finalArray2[2] intValue];

        NSString *strlap =[NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
有人请帮帮我。感谢你的时间。

2 个答案:

答案 0 :(得分:0)

做两件事,而不是在数组中添加格式化字符串_currenttimeinsecond [arrsplit addObject:@(_currenttimeinsecond)];

现在,您已经可以使用现在使用的相同方法在单元格中填充该格式。

通过这种方式,您可以轻松计算减法:

int lap = arrsplit[i] - arrsplit[i-1];

希望这会有所帮助。

答案 1 :(得分:0)

arrsplit试试这个......

try
        {

            con = new SqlConnection(cs.ConDB);
            con.Open();

            cmd = new SqlCommand(@"SELECT RTRIM(sno)as [sno],RTRIM(currDate) as [currDate],
            RTRIM(WtrNm) as [WtrNm],RTRIM(Type) as [Type],(No ) as [No] from WtrTblAllot where currDate like '" + currDate.Text + "%' order by currDate", con);
            SqlDataAdapter myDA = new SqlDataAdapter(cmd);
            DataSet WtrTblAllot = new DataSet();
            myDA.Fill(WtrTblAllot , "WtrTblAllot ");
            dataGridView1.DataSource = WtrTblAllot .Tables["WtrTblAllot "].DefaultView;
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

替换此功能

NSString *str1 = arrsplit[1];
NSArray *arr1 = [str1 componentsSeparatedByString:@" "];
NSString *final1 = [arr1 lastObject];
NSArray *finalArray1 = [final1 componentsSeparatedByString:@":"];

NSString *str2 = arrsplit[0];
NSArray *arr2 = [str2 componentsSeparatedByString:@" "];
NSString *final2 = [arr2 lastObject];
NSArray *finalArray2 = [final2 componentsSeparatedByString:@":"];

int hour = [finalArray1[0] intValue] - [finalArray2[0] intValue];
int min = [finalArray1[1] intValue] - [finalArray2[1] intValue];
int sec = [finalArray1[2] intValue] - [finalArray2[2] intValue];

NSString *lap =[NSString stringWithFormat:@"%02d:%02d:%02d", hours, min, sec];

并替换它以避免崩溃

- (IBAction)Split:(id)sender {


if (_Timer != nil) {
    int seconds = _currenttimeinsecond % 60;
    int minutes = (_currenttimeinsecond / 60) % 60;
    int hours = _currenttimeinsecond / 3600;

    NSString *str =[NSString stringWithFormat:@"Split %02d:%02d:%02d", hours, minutes, seconds];
    [arrsplit addObject:str];

    for(int i = 1 ; i < arrsplit.count; i++){
        NSString *str1 = arrsplit[i];

        NSArray *arr1 = [str1 componentsSeparatedByString:@" "];
        NSString *final1 = [arr1 lastObject];
        NSArray *finalArray1 = [final1 componentsSeparatedByString:@":"];

        NSString *str2 = arrsplit[i-1];
        NSArray *arr2 = [str2 componentsSeparatedByString:@" "];
        NSString *final2 = [arr2 lastObject];
        NSArray *finalArray2 = [final2 componentsSeparatedByString:@":"];

        hours = [finalArray1[0] intValue] - [finalArray2[0] intValue];
        minutes = [finalArray1[1] intValue] - [finalArray2[1] intValue];
        seconds = [finalArray1[2] intValue] - [finalArray2[2] intValue];

        NSString *strlap =[NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

        [arrlap addObject:strlap];
    }

    [tbl reloadData];
}

}